|
There are several classes that provide implementations of the Map Interfaces. The classes that can be used for Maps are explained one by one below in this section.
a. AbstractMap: This class implements most of the Map Interface.
b. HashMap: This class uses a hash table to implement the Map interface thus keeping the execution time constant for the various operations like get ( ) and put ( ) even for larger group of data. HashMap implements Map and extends AbstractMap. It does not add any methods of its own.
c. TreeMap: This class implements the Map interface by using a tree. It also assures the storage of the elements in the ascending Order like the HashMap TreeMap implements SortedMap and extends AbstractMap. It does not define any additional methods of its own.
d. LinkedHashMap: This class extends HashMap Class. LinkedHashMap maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted. You can also create a LinkedHashMap that returns its elements in the order in which they were last accessed.
e. IdentityHashMap: This Class implements AbstractMap and is very similar to HashMap except that it makes use of reference equality whne it compares the elements. It is not very commonly used.
[edit] HashMap Example
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
Map map = new HashMap();
map.put("1", "John");
map.put("2", "Ron");
map.put("3", "Scot");
map.put("4", "Teri");
map.put("5", "Sally");
System.out.println("Name : " + map.get("2"));
System.out.println("Contains Key : " + map.containsKey("3"));
System.out.println("Contains Value : " + map.containsValue("John"));
System.out.println(map.keySet());
}
}
Output
Name : Ron
Contains Key : true
Contains Value : true
[3, 5, 2, 4, 1]
In a HashMap elements are not sorted in a order. Elements are placed in the HashMap according to the hash value of the Key. It is basically to increase the performance of the HashMap so that elements can be easily and quickly be accessed at any point of time. But java provides one more kind of HashMap which is called LinkedHashMap which maintains the insertion order. When you call keySet() on it, returns the keys according to the insertion order. The example below will help you to understand this important point. We will use the same above code but with LinkedHashMap implementation.
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapExample {
public static void main(String[] args) {
Map map = new LinkedHashMap();
map.put("1", "John");
map.put("2", "Ron");
map.put("3", "Scot");
map.put("4", "Teri");
map.put("5", "Sally");
System.out.println("Name : " + map.get("2"));
System.out.println("Contains Key : " + map.containsKey("3"));
System.out.println("Contains Value : " + map.containsValue("John"));
System.out.println(map.keySet());
}
}
Output
Name : Ron
Contains Key : true
Contains Value : true
[1, 2, 3, 4, 5]
[edit] Java TreeMap Example
import java.util.Map;
import java.util.TreeMap;
public class TreeMapExample {
public static void main(String[] args) {
Map map = new TreeMap();
map.put("2", "John");
map.put("1", "Ron");
map.put("5", "Scot");
map.put("4", "Teri");
map.put("3", "Sally");
System.out.println("Name : " + map.get("2"));
System.out.println("Contains Key : " + map.containsKey("3"));
System.out.println("Contains Value : " + map.containsValue("John"));
System.out.println(map.keySet());
}
}
Output
Name : John
Contains Key : true
Contains Value : true
[1, 2, 3, 4, 5]
|