Map in Java | Java Tutorial
Java Map is a part of collections framework. The Map interface is based on key value pair. It maps unique keys to values. The Map interface is not a subtype of the Collection interface. The Map interface acts similar to Collections but a bit different from the rest of the collection types. It can’t contain duplicate keys however duplicate values are allowed.
There are two interfaces for implementing Map in Java.
1. Map
2. SortedMap
There are three classes of Map in Java are
1. HashMap
2. Hashtable
3. TreeMap
4. ConcurrentHashMap
5. LinkedHashMap.
A Map cannot contain duplicate keys and each key can map to at most one value. HashMap and LinkedHashMap allow null key and null value but TreeMap doesn’t allow null key and null value. HashMap has no predictable order but LinkedHashMap TreeMap have predictable order.
HashMap: HashMap implements all of the Map operations and allows null values and one null key. HashMap does not maintain an order of its key-value elements. Therefore, consider to use a HashMap when order does not matter and nulls are acceptable.
Check detailed post on HashMap
LinkedHashMap: LinkedHashMap is the implementation of Map, it inherits HashMap class. It allows null values and null key. It maintains insertion order. So consider using a LinkedHashMap when you want a Map with its key-value pairs are sorted by their insertion order.
Check detailed post on LinkedHashMap
TreeMap: TreeMap is the implementation of Map and SortedMap. It maintains ascending order. It doesn’t allow nulls. So consider using a TreeMap when you want a Map sorts its key-value pairs by the natural order of the keys.
Check detailed post on TreeMap
Points to remember:
- Map doesn’t allow duplicate keys, but it allows duplicate values.
- HashMap and LinkedHashMap allows null keys and null values but TreeMap doesn’t allow any null key or value.
- Map can’t be traversed so you need to convert it into Set using keySet() or entrySet() method.
The Map interface includes methods for basic operations (such as put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views (such as keySet, entrySet, and values).
Methods in Map Interface:
- public Object put(Object key, Object value): This method is used to insert an entry in the map.
- public void putAll(Map m): This method is used to insert specified map in this map.
- public Object remove(Object k): This method is used to delete an entry whose key equals k.
- public Object get(Object k):This method is used to return the value associated with the key k.
- public boolean containsKey(Object k): This method returns true if the invoking map contains k as key. Otherwise returns false.
- public Set keySet(): This method is used to return a Set that contains the keys in the invoking map. This method
- provides a set-view of the keys in the invoking map.
- public Set entrySet(): This method is used to return a Set that contains the entries in the map. The set contains objects of type Map.Entry. This method provides a set-view of the invoking map.
Collection Views
The collection view methods allow a Map to be viewed as a Collection in the following ways.
- keySet – the Set of keys contained in the Map
- values – Collection of values contained in the Map. This Collection is not a Set, because multiple keys can map to the same value
- entrySet – the Set of key-value pairs contained in the Map. The Map interface provides a small nested interface called Map. Entry, the type of the elements in this Set.
Learn Collection in Java
Map.Entry Interface:
Entry is the sub interface of Map. We could access it by Map.Entry. It provides methods to get key and value.
Methods of Map.Entry interface:
- Object getKey() – It is used to obtain key.
- Object getValue() – It is used to obtain value.
Sample Program Using Map.Entry:
package myPackage; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MapsClassA { public static void main(String [] args) { Map<String,String> value = new HashMap<>(); value.put("Name", "Raj"); value.put("Country", "India"); value.put(null, null); value.put("Null Value", null); for(Map.Entry<String, String> en : value.entrySet()) { System.out.println(en.getKey()+" = "+en.getValue()); } } }
Sample Program:
package myPackage; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MapsClassA { public static void main(String [] args) { Map<String,String> value = new HashMap<>(); value.put("Name", "Raj"); value.put("Country", "India"); value.put(null, null); value.put("Null Value", null); System.out.println(value); // To fetch a value, we use get method and pass the key as parameter System.out.println("Value of Name is : "+value.get("Name")); // If we pass a key which is not available - You wont face any exception, output will be null System.out.println("Value of Name1 is : "+value.get("Name1")); // To get the size of the Map System.out.println("Size is : "+value.size()); /*As a Map is not a true collection, there is no direct method for iterating over a map. Instead, we can iterate over a map using its collection views. Any Map’s implementation has to provide three Collection view methods ie., keySet(), values(), and entrySet()*/ //Converting to Set so that we can transverse Set<String> keys = value.keySet(); for(String key:keys) { System.out.println(key + " = " + value.get(key)); } // To get the value System.out.println("Value of Country is : "+value.get("Country")); // To remove the value value.remove("Country"); // Lets see how the value will be after we applied remove in the above statement System.out.println("Value of Country after removed it is : "+value.get("Country")); } }