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.
It provides various classes: HashMap, TreeMap, LinkedHashMap for map implementation. All these classes implements Map interface to provide Map properties to the collection.
Interface | Description |
---|---|
Map | Maps unique key to value. |
Map.Entry | Describe an element in key and value pair in a map. Entry is sub interface 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. |
These are commonly used methods defined by Map interface
get()
and put()
to remain same.HashMap()
HashMap(Map< ? extends k, ? extends V> m)
HashMap(int capacity)
HashMap(int capacity, float fillratio)
Lets take an example to create hashmap and store values in key and value pair. Notice to insert elements, we used put()
method because map uses put to insert element, not add()
method that we used in list interface.
import java.util.*;
class Demo
{
public static void main(String args[])
{
HashMap< String,Integer> hm = new HashMap< String,Integer>();
hm.put("a",100);
hm.put("b",200);
hm.put("c",300);
hm.put("d",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());
}
}
}
a:100 b:200 c:300 d:400
In this example, we are creating treemap to store data. It uses tree to store data and data is always in sorted order. See the below example.
import java.util.*;
class Demo
{
public static void main(String args[])
{
TreeMap<String,Integer> tm = new TreeMap<String,Integer>();
tm.put("a",100);
tm.put("b",200);
tm.put("c",300);
tm.put("d",400);
Set<Map.Entry<String,Integer>> st = tm.entrySet();
for(Map.Entry<String,Integer> me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
a:100 b:200 c:300 d:400
LinkedHashMap()
LinkedHashMap(Map< ? extends k, ? extends V> m)
LinkedHashMap(int capacity)
LinkedHashMap(int capacity, float fillratio)
LinkedHashMap(int capacity, float fillratio, boolean order)
removeEldestEntry()
. This method is called by put()
and putAll()
By default this method does nothing.Here we are using linkedhashmap to store data. It stores data into insertion order and use linked-list internally. See the below example.
import java.util.*;
class Demo
{
public static void main(String args[])
{
LinkedHashMap<String,Integer> tm = new LinkedHashMap<String,Integer>();
tm.put("a",100);
tm.put("b",200);
tm.put("c",300);
tm.put("d",400);
Set<Map.Entry<String,Integer>> st = tm.entrySet();
for(Map.Entry<String,Integer> me:st)
{
System.out.print(me.getKey()+":");
System.out.println(me.getValue());
}
}
}
a:100 b:200 c:300 d:400