Linked HashMap Example Java Program


Definition

HashMap uses a hash table. The hashes of the keys are used to find the values in various buckets. LinkedHashMap extends this by creating a doubly linked list between the elements. This allows the elements to be accessed in the order in which they were inserted into the map.

Syntax

Map Variable_name = new LinkedHashMap();

Linked HashMap Example Program

import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapDemo {
  public static void main(String[] args) {
        Map linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(1, new String("Samsung"));
        linkedHashMap.put(2, new String("Mi"));
        linkedHashMap.put(3, new String("Toshiba"));
        linkedHashMap.put(4, new String("HCL"));
        linkedHashMap.put(5, new String("Wipro"));
        System.out.println("Contents of LinkedHashMap : " + linkedHashMap);
        System.out.println("\nValues of map after iterating over it : ");
        for (Integer key : linkedHashMap.keySet()) {
            System.out.println(key + ":\t" + linkedHashMap.get(key));
        }
        System.out.println("\nThe size of the LinkedHashMap is : " + linkedHashMap.size());
        System.out.println("\nIs LinkedHashMap empty? : " + linkedHashMap.isEmpty());
        System.out.println("\nLinkedHashMap contains 2 as key? : " + linkedHashMap.containsKey(2));
        System.out.println("LinkedHashMap contains HCL as value? : " + linkedHashMap.containsValue("HCL"));
        System.out.println("\nRemove entry for key 3 : " + linkedHashMap.remove(3));
        System.out.println("Content of LinkedHashMap after removing key 2: " + linkedHashMap);
        linkedHashMap.clear();
        System.out.println("\nContent of LinkedHashMap after clearing: " + linkedHashMap);
    }
}

Sample Output

Output is:
Contents of LinkedHashMap : {1=Samsung, 2=Mi, 3=Toshiba, 4=HCL, 5=Wipro}

Values of map after iterating over it :
1:      Samsung
2:      Mi
3:      Toshiba
4:      HCL
5:      Wipro

The size of the LinkedHashMap is : 5

Is LinkedHashMap empty? : false

LinkedHashMap contains 2 as key? : true
LinkedHashMap contains HCL as value? : true

Remove entry for key 3 : Toshiba
Content of LinkedHashMap after removing key 2: {1=Samsung, 2=Mi, 4=HCL, 5=Wipro}

Content of LinkedHashMap after clearing: {}