Home » Java programming language

Differences between HashMap and LinkedHashMap in Java

Java HashMap vs LinkedHashMap: Here, we are going to learn what are the differences between HashMap and LinkedHashMap in Java programming language?
Submitted by Preeti Jain, on August 04, 2019

HashMap vs LinkedHashMap

First, we will see how LinkedHashMap differs from HashMap in Java?

LinkedHashMap in Java

  • This class is available in java.util package.
  • LinkedHashMap is a child class of HashMap.
  • LinkedHashMap is an implementation class of Map interface.
  • The underlying data structure is a combination of Hashtable and LinkedList.
  • In LinkedHashMap, "insertion order of the elements is preserved" that means the insertion order of the elements must be the same as the order of retrieving the elements.
  • In LinkedHashMap, "duplicate values are allowed but keys are not allowed".
  • This class is introduced in 1.4 version.
  • We should go for LinkedHashMap where the insertion order of the element is important.

Example:

Let suppose we have a HashMap with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=10000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e. it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000, null=10000, Django=null}(i.e. order of insertion and retrieval will be same because insertion order is preserved).

// Java program to demonstrate the behavior of LinkedHashMap
import java.util.Collection;
import java.util.LinkedHashMap;

class LinkedHashMapClass {
    public static void main(String[] args) {
        // Creating an instance of LinkedHashMap
        LinkedHashMap lhm = new LinkedHashMap();

        //By using put() method to add some values in LinkedHashMap
        lhm.put("Java", 1000);
        lhm.put("C", 2000);
        lhm.put("C++", 3000);
        lhm.put("Ruby", 4000);
        lhm.put("Python", 1000);
        lhm.put("null", null);
        lhm.put("Django", null);

        /* Here one null will be accepted for keys */
        lhm.put("null", 10000);

        // Display retrieval order of HashMap
        System.out.println("Current LinkedHashMap list is :" + lhm);

        // by using values() to find values of HashMap
        Collection values = lhm.values();

        // Display Values of HashMap
        System.out.println("Current LinkedHashMap Key values is :" + values);
    }
}

Output

E:\Programs>javac LinkedHashMapClass.java

E:\Programs>java LinkedHashMapClass
Current LinkedHashMap list is :{Java=1000, C=2000, C++=3000, Ruby=4000, 
Python=1000, null=10000, Django=null}
Current HashMap Key values is :[1000, 2000, 3000, 4000, 1000, 10000, null]

Second, we will see how HashMap differs from LinkedHashMap in Java?

HashMap in Java

  • This class is also available in java.util package.
  • HashMap is a parent class of LinkedHashMap.
  • HashMap is an implementation class of Map interface.
  • The underlying data structure is a combination of Hashtable.
  • In HashMap, "insertion order of the elements is not preserved" that means the insertion order of the elements are not needed to be the same as the order of retrieving the elements.
  • In HashMap, "duplicate values are allowed but keys are not allowed".
  • This class is introduced in 1.2 version.
  • We should go for HashMap where the insertion order of the element is not important.

Example:

Let suppose we have a HashMap with few elements. Here we are adding the elements in the order is {Java=1000, C=2000, C++=3000, Ruby=4000, Python=1000,null=null, Django=null, null=7000} and if we are retrieving the elements so the order of retrieving elements can be different (i.e.insertion order is not preserved and it is not needed to be the same insertion and retrieval order of the elements.) so the output will be different and the order will be like {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=7000, Java=1000}

// Java program to demonstrate the behavior of HashMap
import java.util.Collection;
import java.util.HashMap;

class HashMapClass {
    public static void main(String[] args) {
        // Creating an instance of HashMap
        HashMap hm = new HashMap();

        //By using put() method to add some values in HashMap
        hm.put("Java", 1000);
        hm.put("C", 2000);
        hm.put("C++", 3000);
        hm.put("Ruby", 4000);
        hm.put("Python", 1000);
        hm.put("null", null);
        hm.put("Django", null);

        // Here we will not get any error but one null is accepted for keys
        hm.put("null", 7000);

        // Display retrieval order of HashMap
        System.out.println("Current HashMap list is :" + hm);

        // by using values() to find values of HashMap
        Collection values = hm.values();

        // Display Values of HashMap
        System.out.println("Current HashMap Key values is :" + values);
    }
}

Output

E:\Programs>javac HashMapClass.java

E:\Programs>java HashMapClass
Current HashMap list is :{Ruby=4000, C=2000, Django=null, Python=1000, 
C++=3000, null=7000, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, 7000, 1000]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.