×
☰ See All Chapters

Java Map

  • A map is an object that stores associations between keys and values, or key/value pairs. Given a key, you can find its value. Both keys and values are objects.  

  • The keys must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot. 

  • The Map interface maps unique keys to values. A key is an object that we use to retrieve a value at a later date. Given a key and a value, we can store the value in a Map object. After the value is stored, we can retrieve it by using its key. 

  • Maps revolve around two basic operations: get( ) and put( ). To put a value into a map, use put( ), specifying the key and the value. To obtain a value, call get( ), passing the key as an argument. The value is returned. 

  • Although part of the Collections Framework, maps are not, themselves, collections because they do not implement the Collection interface. However, you can obtain a collection-view of a map. To do this, we can use the entrySet( ) method, to obtain a collection-view of the keys, use keySet( ). 

  • They don’t implement the Iterable interface. This means that we cannot cycle through a map using a for-each style for loop and we can’t obtain an iterator to a map. However we can obtain a collection-view of a map, which does allow the use of either the for loop or an iterator. 

  • There are many different implementations of the Map interface. 

    1. TreeMap. 

    2. HashMap. 

    3. LinkedHashMap. 

    4. Hashtable. 

    5. Properties 

TreeMap

  • TreeMap is implementing SortedMap interface. 

  • TreeMap uses tree data structure to store the keys. 

  • The keys are stored in sorted order. 

  • Only similar types of keys are allowed. 

  • Different types of values are allowed. 

  • Duplicate keys are not allowed but duplicate values are allowed. 

  • Null keys are not allowed but null values. 

java-map-0
 
java-map-1
 

TreeMap Example

TreeMapDemo.java

import java.util.SortedMap;

import java.util.TreeMap;

 

public class TreeMapDemo {

 

        public static void main(String[] args) {

                // Create a TreeMap

                TreeMap map1 = new TreeMap();

                TreeMap map2 = new TreeMap();

                // Add key/value to the TreeMap

                // TreeMap stores keys in sorted order

                map1.put(new Integer(103), "CCC");

                map1.put(new Integer(105), "EEE");

                map1.put(new Integer(102), "AAA");

                map1.put(new Integer(101), "BBB");

                map1.put(new Integer(104), "DDD");

 

                map2.put("Manu", new Double(11.11));

                map2.put("Likitha", new Double(22.22));

                map2.put("Advith", new Double(33.33));

                map2.put("Manjunatha", new Double(44.44));

                // using same key will update or replace the value

                map2.put("Manu", new Double(99.99));

 

                // can add different type of value in TreeMap

                map2.put("Hello", "HELLO");

 

                // can put null value in TreeMap

                map2.put("Hello", null);

 

                // cannot add null key in TreeMap

                // tm.put(null, "CLUSTER");

 

                // cannot add different type of keys in TreeMap

                // tm.put(new Integer(10),new Integer(20));

                System.out.println(map1);

                System.out.println(map2);

                // getting a value using a key

                Object e = map2.get("Manu");

                System.out.println("Value of Manu is: " + e);

 

                Object e1 = map2.get("Likitha");

                System.out.println("Value of Likitha is: " + e1);

 

                Object e2 = map2.get("Tom");

                System.out.println("Value of Tom is: " + e2);

 

                boolean b = map2.containsKey("Manu");

                boolean b1 = map2.containsKey("Raju");

                System.out.println("Key Manu is present: " + b);

                System.out.println("Key Raju is present: " + b1);

 

                System.out.println(map2.containsValue("33.33"));

                System.out.println(map2.containsValue(new Double(33.33)));

 

                // gives key/value from first to 50 and excludes 50

                SortedMap hm = map1.headMap(new Integer(50));

                System.out.println("Values of headMap: " + hm);

 

                // gives key/value from 30 to last and includes 30

                SortedMap tm = map1.tailMap(new Integer(30));

                System.out.println("Values of tailMap: " + tm);

 

                // gives key/value including start range 30 till end range excluding 70

                SortedMap sm = map1.subMap(new Integer(30), new Integer(70));

                System.out.println("Values of subMap: " + sm);

 

                // deleting

                map2.remove("Hello");

                System.out.println("Values in TreeMap after deleting: " + map1);

 

                map2.clear();

                System.out.println("Values in TreeMap after clearing: " + map1);

 

        }

}

Output:

public class Test {

{101=BBB, 102=AAA, 103=CCC, 104=DDD, 105=EEE}

{Advith=33.33, Hello=null, Likitha=22.22, Manjunatha=44.44, Manu=99.99}

Value of Manu is: 99.99

Value of Likitha is: 22.22

Value of Tom is: null

Key Manu is present: true

Key Raju is present: false

false

true

Values of headMap: {}

Values of tailMap: {101=BBB, 102=AAA, 103=CCC, 104=DDD, 105=EEE}

Values of subMap: {}

Values in TreeMap after deleting: {101=BBB, 102=AAA, 103=CCC, 104=DDD, 105=EEE}

Values in TreeMap after clearing: {101=BBB, 102=AAA, 103=CCC, 104=DDD, 105=EEE}

HashMap

 HashMap uses hash table data structure to store the keys.

 HashMap defines no any extra methods other than those inherited from Map Interface.

LinkedHashMap

      • LinkedHashMap store the keys in hash table data structure and links them with double linked list. 

      • LinkedHashMap defines no any extra methods other than those inherited from Map Interface. 

Hashtable

      • Hashtable can store key/value pairs. 

      • It uses hash table data structure to store the keys. 

      • Hashtable is a legacy class (Java 1.0) and it was extending Dictionary class. 

      • Hashtable is re-engineered to fit into collection framework and is implementing Map interface and hence supports both 1.0 methods and 1.2 methods. 

      • Hashtable is same as HashMap, but Hashtable is synchronized and HashMap is not synchronized. 

      • Hashtable is synchronized; multiple threads cannot access the Hashtable object concurrently. Only one thread can access the Hashtable object at a specific time. 

      • Since Hashtable is synchronized, it has low performance than HashMap. 

      • Before to java 1.2 there was an interface called Enumeration to visit the elements of Hashtable. But now we have Iterator interface to visit the elements of Hashtable. 

      • The keys will be unordered or undefined order. 

      • Duplicate values are allowed. 

      • Different types of keys and values are allowed. 

      • Null keys and null values are not allowed. 

 

 


All Chapters
Author