Javatpoint Logo
Javatpoint Logo

Java Map Generic

Java is one of the most popular programming languages in the world, known for its versatility and wide range of applications. One of the most powerful features of Java is its collection framework, which includes classes and interfaces for managing collections of objects. One of the most useful classes in the collection framework is the Map interface, which allows us to store and retrieve key-value pairs.

The Map interface is a generic type, which means that you can create a Map object that can hold any type of key and any type of value. This makes the Map interface incredibly flexible and versatile, as you can use it to store and retrieve data of any type.

To create a Map object, you can use one of the concrete classes that implement the Map interface, such as HashMap, TreeMap, or LinkedHashMap. Each of these classes has its own unique features and characteristics, but they all provide the basic functionality of a map.

The basic syntax for creating a Map object is as follows:

In this example, "KeyType" and "ValueType" are the types of the key and value that you want to store in the map. The HashMap class is used to create the Map object.

To add values to the map, you can use the put() method, which takes a key and a value as parameters and adds them to the map. For example:

In this example, the keys are strings and the values are also strings. However, you can use any type of object as a key or value, as long as it is consistent throughout the map.

To retrieve values from the map, you can use the get() method, which takes a key as a parameter and returns the value associated with that key. For example:

In this example, the values "value1" and "value2" are retrieved from the map using their respective keys.

One of the most powerful features of the Map interface is its ability to iterate over the keys or values in the map using the keySet() and values() methods. For example:

In this example, the keySet() method is used to retrieve a set of all the keys in the map, and then a for loop is used to iterate over the keys and retrieve the corresponding values using the get() method.

here's an example program in Java that takes user input for a shopping list and outputs the list along with the total cost:

ShoppingList.java

Output:

Enter items and prices for your shopping list:
Item: Apples
Price: $2.49
Item: Bread
Price: $1.99
Item: Milk
Price: $3.29
Item: 
Your shopping list:
Apples - $2.49
Bread - $1.99
Milk - $3.29
Total: $7.77

This program uses a Map to store the user's shopping list items and their corresponding prices. It first prompts the user to enter items and prices, and then loops until the user inputs a blank line. Each item and price is added to the Map using the put() method.

After the user has entered their shopping list, the program loops over the Map using the keySet() method to retrieve each item and its corresponding price. It then prints each item and price to the console using printf() to format the output with a dollar sign and two decimal places. The total cost of all items is also calculated and printed at the end.

Map interface in Java is a powerful and versatile tool for storing and retrieving key-value pairs of any type. Its generic type allows it to be used with any type of object, and its various concrete implementations provide different features and characteristics for different use cases. Whether you are working on a small project or a large enterprise application, the Map interface is an essential tool for managing collections of data in Java.


Next TopicJava NLP





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA