The java.util.Map interface represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit different from the rest of the collection types.
Map Implementations
Since Map is an interface you need to instantiate a concrete implementation of the interface in order to use it. You can choose between the following Map implementations in the Java Collections API:
java.util.HashMap java.util.Hashtable java.util.EnumMap java.util.IdentityHashMap java.util.LinkedHashMap java.util.Properties java.util.TreeMap java.util.WeakHashMap
Usually, the most commonly used Map implementations are HashMap and TreeMap.
Each of these Map implementations behaves a little differently with respect to the order of the elements when iterating the Map, and the time (big O notation) it takes to insert and access elements in the maps.
HashMap maps a key and a value. It does not guarantee any order of the elements stored internally in the map.
TreeMap also maps a key and a value. Furthermore it guarantees the order in which keys or values are iterated - which is the sort order of the keys or values. Check out the JavaDoc for more details.
Here are a few examples of how to create a Map instance:
Map mapA = new HashMap(); Map mapB = new TreeMap();
Adding and Accessing Elements
To add elements to a Map you call its put() method. Here are a few examples:
Map mapA = new HashMap();
mapA.put("key1", "element 1"); mapA.put("key2", "element 2"); mapA.put("key3", "element 3");
The three put() calls maps a string value to a string key. You can then obtain the value using the key. To do that you use the get() method like this:
String element1 = (String) mapA.get("key1");
You can iterate either the keys or the values of a Map. Here is how you do that:
// key iterator Iterator iterator = mapA.keySet().iterator();
// value iterator Iterator iterator = mapA.values();
Most often you iterate the keys of the Map and then get the corresponding values during the iteration. Here is how it looks:
Iterator iterator = mapA.keySet().iterator(); while(iterator.hasNext(){ Object key = iterator.next(); Object value = mapA.get(key); }
//access via new for-loop for(Object key : mapA.keySet()) { Object value = mapA.get(key); }
Removing Elements
You remove elements by calling the remove(Object key) method. You thus remove the (key, value) pair matching the key.
Generic Maps
By default you can put any Object into a Map, but from Java 5, Java Generics makes it possible to limit the types of object you can use for both keys and values in a Map. Here is an example:
Mapmap = new HashMap ();
This Map can now only accept String objects for keys, and MyObject instances for values. You can then access and iterate keys and values without casting them. Here is how it looks:
for(MyObject anObject : map.values()){ //do someting to anObject... }
for(String key : map.keySet()){ MyObject value = map.get(key); //do something to value }
Useful methods of Map interface
- Object put(Object key, Object value): It is used to insert an entry in this map.
- void putAll(Map map): It is used to insert the specified map in this map.
- Object remove(Object key): It is used to delete an entry for the specified key.
- Object get(Object key): It is used to return the value for the specified key.
- boolean containsKey(Object key): It is used to search the specified key from this map.
- Set keySet(): It is used to return the Set view containing all the keys.
- Set entrySet(): It is used to return the Set view containing all the keys and values.
Map.Entry Interface
Entry is the sub interface of Map. So we will be accessed it by Map.Entry name. 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.
Java Map Example: Generic (New Style)
import java.util.*; class MapInterfaceExample{ public static void main(String args[]){ Mapmap=new HashMap (); map.put(100,"Amit"); map.put(101,"Vijay"); map.put(102,"Rahul"); for(Map.Entry m:map.entrySet()){ System.out.println(m.getKey()+" "+m.getValue()); } } }
Output:
102 Rahul 100 Amit 101 Vijay
Java Map Example: Non-Generic (Old Style)
//Non-generic import java.util.*; public class MapExample1 { public static void main(String[] args) { Map map=new HashMap(); //Adding elements to map map.put(1,"Amit"); map.put(5,"Rahul"); map.put(2,"Jai"); map.put(6,"Amit"); //Traversing Map Set set=map.entrySet();//Converting to Set so that we can traverse Iterator itr=set.iterator(); while(itr.hasNext()){ //Converting to Map.Entry so that we can get key and value separately Map.Entry entry=(Map.Entry)itr.next(); System.out.println(entry.getKey()+" "+entry.getValue()); } } }
Output:
1 Amit 2 Jai 5 Rahul 6 Amit