What is Map interface in JAVA?
The Map interface, which is part of java.util package, stores elements in key-value pair. A key is an object that we use to retrieve a value later. The keys must be unique, but the values may be duplicated. Some maps can accept a null key and null values(HashMap & LinkedHashMap), others (TreeMap) cannot. Map is declared as shown below:
Interface Map<K, V>
Here, K specifies the type of keys, and V specifies the type of values.
Although Maps are part of Collection Framework, these do not extend Collection interface. The reason is the mechanism to store object in Collection interface and Map interface is different.
Since Map is an interface, we need implementing classes of Map interface (or child interface of Map) to create Map object. The most common classes which provide the concrete implementation of Map are HashMap, LinkedHashMap & TreeMap.
How to add elements to a Map?
Let’s use HashMap class to create a Map and see how we can add elements to it. We need to use put() method to add elements to a map.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "One");
hashmap.put(2, "Two");
hashmap.put(null, null);
System.out.println(hashmap);
}
}
Output
{null=null, 1=One, 2=Two}
Explanation
Unlike other Collection classes, we need to provide both key & value type (Integer & String in the example) while declaring the Map.
Also, notice that null is accepted as key as well as value.
How to remove element from Map?
We need to use remove() method and pass they key which we need to remove. When the key is removed the corresponding value is removed too.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "One");
hashmap.put(2, "Two");
hashmap.put(3, "Three");
System.out.println(hashmap);
hashmap.remove(3);
System.out.println(hashmap);
}
}
Output
{1=One, 2=Two, 3=Three}
{1=One, 2=Two}
How can we modify an element in a Map?
After adding the elements if we wish to change the element, it can be done by again by adding the element using the put() method.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "One");
hashmap.put(2, "Two");
hashmap.put(3, "Three");
System.out.println(hashmap);
hashmap.put(3, "Updated_Three");
System.out.println(hashmap);
}
}
Output
{1=One, 2=Two, 3=Three}
{1=One, 2=Two, 3=Updated_Three}
Explanation
In the program above, we have updated the value of key 3 to Updated_Three by inserting it again in the map.
How do we iterate over map?
Since Map interface does not extend Collection interface, we cannot call iterate() method on a Map to iterate all the elements.
We can use entrySet() method of Map interface which returns a collection view of the Map.
The collection view is stored in an interface called Map.Entry. Map.Entry offers methods getKey() & getValue(), which are used to get the keys and values of the Map.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> hashmap = new HashMap<>();
hashmap.put(1, "One");
hashmap.put(2, "Two");
hashmap.put(3, "Three");
for (Map.Entry <Integer, String > mapElement : hashmap.entrySet()) {
int key = mapElement.getKey();
String value = mapElement.getValue();
System.out.println(key + " : " + value);
}
}
}
Output
1 : One
2 : Two
3 : Three
What is LinkedHashMap?
LikedHashMap extends HashMap with additional feature of maintaining the insertion order.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> linkedhashmap = new LinkedHashMap<>();
linkedhashmap.put(1, "Sam");
linkedhashmap.put(3, "John");
linkedhashmap.put(2, "Terry");
for (Map.Entry<Integer, String> mapElement : linkedhashmap.entrySet()) {
System.out.println(mapElement.getKey() + " : " + mapElement.getValue());
}
}
}
Output
1 : Sam
3 : John
2 : Terry
Explanation
Since we used LinkedHashMap for concrete implementation, the output shows that the insertion order is maintained.
What is TreeMap and when should it be used?
TreeMap class implements NavigableMap interface, which extends SortedMap, which extends Map.
The unique feature TreeMap provides is, it is sorted based on the keys. Let’s see an example.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> treemap = new TreeMap<>();
treemap.put(1, "Sam");
treemap.put(3, "John");
treemap.put(2, "Terry");
treemap.put(5, "Alpha");
treemap.put(4, "Charlie");
for (Map.Entry<Integer, String> mapElement : treemap.entrySet()) {
System.out.println(mapElement.getKey() + " : " + mapElement.getValue());
}
}
}
Output
1 : Sam
2 : Terry
3 : John
4 : Charlie
5 : Alpha
Explanation
Even though the insertion order is random, TreeMap sorted the elements based on the key as shown in the output.
How to fetch all the keys & all the values from a Map?
We can use keySet() and values() methods respectively, to get all the keys and values from a Map.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, String> treemap = new TreeMap<>();
treemap.put(1, "Sam");
treemap.put(3, "John");
treemap.put(2, "Terry");
treemap.put(5, "Alpha");
treemap.put(4, "Charlie");
Set<Integer> keys = treemap.keySet();
Collection<String> values = treemap.values();
for (int x : keys) {
System.out.println(x);
}
System.out.println();
for (String y : values) {
System.out.println(y);
}
}
}
Output
1
2
3
4
5
Sam
Terry
John
Charlie
Alpha