What is the differences between TreeMap, HashMap and LinkedHashMap in Java?

Java 8Object Oriented ProgrammingProgramming

HashSet and ArrayList both are one of the most important classes of the Java Collection framework.

The following are the important differences between TreeMap, HashMap and LinkedHashMap.

Sr. No.KeyTreeMapHashMapLinkedHashMap
1Ordering of elementsThe elements inserted in TreeMap are sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.In case of HashMap it does not guarantees as to the order of the map also it does not guarantee that the order will remain constant over time.LinkedHashMap follows the insertion order of elements and also maintaining an order of elements inserted into it.
2Internal implementationThe internal implementation of TreeMap does not allow storing null key but only null values are allowed.In the case of HashMap storing one null key as well as of multiple null values are allowed.LinkedHashmap is internally implemented more similar as HashMap so storing of one null key and of multiple null values are allowed.
3Operational ComplexityTreeMap comes with the complexity of its get,put and remove operations as O(log(n)), which is greater than that of HashMapHashMap on other hand has the complexity of O(1) in case of its get,put and remove operations.LinkedHashMap again has the same complexity as of HashMap i.e O(1).
4InheritanceTreeMap implements SortedMap interface of Collection framework which is a child of Map.And internally TreeMap implements Red-Black Tree(a Self Balancing Binary Search Tree).On other hand HashMap implements simple Map interface and internally uses hashing for storing and retrieval of its elements.Alike of TreeMap LinkedHashMap extends HashMap and internally uses hashing as like in HashMap.
5Index performanceTreeMap is maintaining order of its elements hence is lesser in performance index and also requires more memory than HashMap and LinkedHashMap.HashMap as do not maintain any insertion order of its elements hence is faster as compare to TreeMap also do not sort its elements on the basis of its value so also faster than LinkedHashMap.LinkedHashMap is faster as compare to TreeMap but is slower than HashMap.
6ComparisonElements in TreeMap get compared by using compareTo() method in which custom implementation could also be provided.On other hand HashMap uses compare() method of Object class for its elements comparison.LinkedHashMap also uses compare() method of Object class for its elements comparison.

Example of TreeMap vs HashMap vs LinkedHashMap

JavaTester.java

// Java program to print ordering
// of all elements using HashMap
import java.util.*;
import java.lang.*;
import java.io.*;
public class JavaTester{
   static void getAndShow(AbstractMap map){
      int[] input= {1, -1, 0, 2,-2};
      for (int x: input){
         map.put(x, Integer.toString(x));
      }
      Set<Integer> keySet = map.keySet();
      for (int k: keySet){
         System.out.print(k + ", ");
      }
   }
   public static void main (String[] args){
      HashMap map = new HashMap();
      getAndShow(map);
      LinkedHashMap map1 = new LinkedHashMap();
      getAndShow(map1);
      TreeMap map2 = new TreeMap();
      getAndShow(map2);
   }
}

Output

-1, 0, 1, -2, 2, 1, -1, 0, 2, -2, -2, -1, 0, 1, 2,
raja
Published on 18-Sep-2019 13:54:20
Advertisements