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.

InterfaceDescription
MapMaps unique key to value.
Map.EntryDescribe an element in key and value pair in a map. This is an inner class of map.
NavigableMapExtends SortedMap to handle the retrienal of entries based on closest match searches
SortedMapExtends Map so that key are maintained in an ascending order.

map interface sub interfaces


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

  1. HashMap class extends AbstractMap and implements Map interface.
  2. It uses a hashtable to store the map. This allows the execution time of get() and put() to remain same.
  3. HashMap has four constructor.
    HashMap()
    HashMap(Map< ? extends k, ? extends V> m)
    HashMap(int capacity)
    HashMap(int capacity, float fillratio)
    
  4. 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

  1. TreeMap class extends AbstractMap and implements NavigableMap interface.
  2. It creates Map, stored in a tree structure.
  3. A TreeMap provides an efficient means of storing key/value pair in efficient order.
  4. 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.Entry me:st)  
  {
   System.out.print(me.getKey()+":");
   System.out.println(me.getValue());
  }
 }
}

Output :

a 100
b 200
c 300
d 400

LinkedHashMap class

  1. LinkedHashMap extends HashMap class.
  2. It maintains a linked list of entries in map in order in which they are inserted.
  3. 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)
    
  4. It adds one new method removeEldestEntry(). This method is called by put() and putAll() By default this method does nothing. However we can override this method to remove oldest element in the map. Syntax
    protected boolean removeEldestEntry(Map.Entry e)
    

EnumMap class

  1. EnumMap extends AbstractMap and implements Map interface.
  2. It is used for key as enum

Quantitative Aptitude
Reasoning
Programming
Interview