Map Interface
A Map stores data in key and value association. Both key and values are objects. The key must be unique but the values can be duplicate. Although Maps are a part of Collection Framework, they can not actually be called as collections because of some properties that they posses. However we can obtain a collection-view of maps.
Interface | Description |
---|---|
Map | Maps unique key to value. |
Map.Entry | Describe an element in key and value pair in a map. This is an inner class of map. |
NavigableMap | Extends SortedMap to handle the retrienal of entries based on closest match searches |
SortedMap | Extends Map so that key are maintained in an ascending order. |
Commonly used Methods defined by Map
- boolean containsKey(Object k): returns true if map contain k as key. Otherwise false.
- Object get(Object k) : returns values associated with the key k.
- Object put(Object k, Object v) : stores an entry in map.
- Object putAll(Map m) : put all entries from m in this map.
- Set keySet() : returns Set that contains the key in a map.
- Set entrySet() : returns Set that contains the entries in a map.
HashMap class
- HashMap class extends AbstractMap and implements Map interface.
- It uses a hashtable to store the map. This allows the execution time of
get()
andput()
to remain same. - HashMap has four constructor.
HashMap() HashMap(Map< ? extends k, ? extends V> m) HashMap(int capacity) HashMap(int capacity, float fillratio)
- HashMap does not maintain order of its element.
Example
import java.util.*;
class HashMapDemo
{
public static void main(String args[])
{
HashMap< String,Integer> hm = new HashMap< String,Integer>();
hm.put("a",new Integer(100));
hm.put("b",new Integer(200));
hm.put("c",new Integer(300));
hm.put("d",new Integer(400));
Set< Map.Entry< String,Integer> > st = hm.entrySet(); //returns Set view
for(Map.Entry< String,Integer> me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
Output :
c 300 a 100 d 400 b 200
TreeMap class
- TreeMap class extends AbstractMap and implements NavigableMap interface.
- It creates Map, stored in a tree structure.
- A TreeMap provides an efficient means of storing key/value pair in efficient order.
- It provides key/value pairs in sorted order and allows rapid retrieval.
Example
import java.util.*; class TreeMapDemo { public static void main(String args[]) { TreeMap< String,Integer> tm = new TreeMap< String,Integer>(); tm.put("a",new Integer(100)); tm.put("b",new Integer(200)); tm.put("c",new Integer(300)); tm.put("d",new Integer(400)); Set< Map.Entry< String,Integer> > st = tm.entrySet(); for(Map.Entryme:st) { System.out.print(me.getKey()+":"); System.out.println(me.getValue()); } } }
Output :
a 100 b 200 c 300 d 400
LinkedHashMap class
- LinkedHashMap extends HashMap class.
- It maintains a linked list of entries in map in order in which they are inserted.
- LinkedHashMap defines the following constructor
LinkedHashMap() LinkedHashMap(Map< ? extends k, ? extends V> m) LinkedHashMap(int capacity) LinkedHashMap(int capacity, float fillratio) LinkedHashMap(int capacity, float fillratio, boolean order)
- It adds one new method
removeEldestEntry()
. This method is called byput()
andputAll()
By default this method does nothing. However we can override this method to remove oldest element in the map. Syntaxprotected boolean removeEldestEntry(Map.Entry
e)
EnumMap class
- EnumMap extends AbstractMap and implements Map interface.
- It is used for key as enum