A Map is an in-built interface. It is part of the Collection framework. It can be used to store a group of key-value pairs. In this article, we will be learning more about Maps and understanding the various operations that can be performed on a Map.
- What is a Map
- Creating a Map
- Adding Data to a Map
- Deleting Data from a Map
- Determining Map Size
- Retrieving Value by Key
- Map Iterations
- Other Map Operations
What is a Map
As mentioned earlier, Map is an interface in the Collection framework. It can be used to store key-value pairs. The keys in a Map are unique, however, the values associated with the keys can be repeated. There are many classes that implement the Map interface like HashMap, TreeMap, and LinkedHashMap.
Creating a Map
In order to create a Map, you need to create an object of any of the classes that implement the Map interface like HashMap, TreeMap, etc. You also need to specify the data type of both the key and the value via generics.
Code Sample
Map<Integer,String> map1 = new HashMap<Integer,String>(); Map<Long, Double> map2 = new TreeMap<Long, Double>();
- Line 1 creates a Map called map1. It stores keys of type Integer and values of type String. It uses a HashMap implementation
- Line 2 creates a Map called map2. It stores keys of type Integer and values of type Double. It uses a TreeMap implementation
- Both the key and the value can be custom classes
Adding Data to the Map
The Map interface has a method called put. This can be used to add data to the Map. It accepts as a parameter the key and the value to be added to the Map.
Code Sample
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); flowers.put(2, "Sunflower"); System.out.println(flowers);
- Line 1 creates a Map called flowers that stores keys of type Integer and values of type String
- Line 2 invokes the put method. It specifies the key as 1 and value as “Rose”. So basically, the value “Rose” is added with the key as 1
- Similarly, Lines 3-4 add some other key-value pairs to the Map
- Line 5 adds the value “Sunflower” with the key 2. As mentioned earlier, a Map does not allow duplicate keys. Since the key 2 is already present with the value “Lily”, Line 5 causes the value to be overwritten with the value “Sunflower”.
Output
{1=Rose, 2=Sunflower, 3=Tulip}
Deleting Data from a Map
Just like put, there is a remove method on the Map interface. This can be used to remove a key-value pair from the Map. It accepts as parameter a key and removes the key-value pair corresponding to the specified key.
Code Sample
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); System.out.println("Before removing 2-Lily:"+flowers); flowers.remove(2); System.out.println("After removing 2-Lily:"+flowers);
Line 6 uses the remove method with the value 2. This causes the key-value pair 2-Lily to be removed from the Map
Output
Before removing 2-lily:{1=Rose, 2=Lily, 3=Tulip}
After removing 2-lily:{1=Rose, 3=Tulip}
There is another overloaded version of the remove method that accepts both a key and a value and removes the key-value pair only if the specified key is mapped to the specified value.
Determining Map Size
The Map interface has a method called size. This returns the size of the Map or the number of key-value pairs in the Map
Code Sample
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); int flowersMapSize = flowers.size(); System.out.println("Size of Flowers Map is "+flowersMapSize);
Line 5 invokes the size method to obtain the Map size and assigns it to the flowersMapSize variable
Output
Size of Flowers Map is 3
Retrieving Value by Key
The Map Interface has a method called get. This accepts as parameter a key and returns the value corresponding to the specified key.
Code Sample
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); String flower3 = flowers.get(3); System.out.println("Value corresponding to 3: "+flower3);
Line 5 uses the flowers.get with the value 3. This retrieves the value corresponding to the key 3 which is “Tulip” and assigns it to the flower3 variable.
Output
Value corresponding to 3: Tulip
Map Iteration
There are several ways to iterate over a Map.
Code Sample – Iterating via keys
The Map interface has a method called keySet. This returns a Set corresponding to the keys in the Map which can be used to iterate over the Map.
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); Set keys = flowers.keySet(); for(Integer key:keys) { String value = flowers.get(key); System.out.println(key+":"+value); }
- Line 5 invokes the keySet method which returns a Set of Integer values corresponding to all the keys in the Map.
- Line 6 uses a for-each loop that iterates over the keys Set
- Line 7 invokes the get method to retrieve a value based on its key
Output
1:Rose
2:Lily
3:Tulip
Code Sample – Iterating via values
The Map class has a method called values. This returns a Collection corresponding to the values in the Map which can then be used to iterate over the Map.
Map<Integer,String> flowers = new HashMap<Integer,String>(); flowers.put(1, "Rose"); flowers.put(2, "Lily"); flowers.put(3, "Tulip"); Collection values = flowers.values(); for(String flower:values) { System.out.println(flower); }
- Line 5 invokes the values method which returns a Collection of Strings corresponding to all the values in the Map.
- Line 6 uses a for-each loop that iterates over the values Collection
Output
Rose
Lily
Tulip
Other Map Operations
In addition to the operations listed above, there are several methods on the Map interface that help to perform various operations. These are as listed in the table below:
- clear – Deletes all the key-value pairs from the Map
- containsKey – Returns a boolean value indicating whether the Map contains the specified key or not
- containsValue – Returns a boolean value indicating whether the Map contains the specified value or not
- getOrDefault – Returns the value corresponding to the specified key if present or returns the specified default value
- isEmpty – Returns a boolean value which indicates whether the Map contains any key-value pairs or not
- replace – Accepts as parameter a key and a value. Replaces the value corresponding to the specified key if present with the specified value. If the key is already present, it returns its current value, otherwise it returns a null.
- putAll – Accepts as parameter a Map and adds all the values from the specified Map to the Map on which it is invoked
- putIfAbsent – Accepts as parameter a key and a value. It inserts the specified key-value pair only if the key is absent. If the key is already present, it returns its current value, otherwise it returns a null.
Conclusion
So, in this article we learnt about Java Maps and some of the operations that can be performed on a Map. We saw how Maps can be used to store key-value pairs. We also saw some of the operations that can be performed on the Map like inserting data into the Map, removing data from a Map, obtaining the size of the Map and iterating over a Map.