HashMap vs LinkedHashMap vs TreeMap vs HashTable in Java

All four (HashMap, LinkedHashMap, TreeMap, HashTable) in Java, implements the Map interface. With this similarity, they have some differences also.

Map Hierarchy:

HashMap:

HashMap contains the objects in key-value pair form. It extends AbstractMap class and implements the Map interface. It does not maintains any order for it’s objects. Duplicate keys are not allowed in HashMap. It can have only one null as key but can have multiple null as values.

Example

import java.util.*; 
public class Main {  
 
    public static void main(String args[]) 
    { 
        //Create HashMap Object
        HashMap<Integer,String> students = new HashMap<Integer,String>();
 
        //Add objects in HashMap
        students.put(1, "Jai");
        students.put(2, "Mahesh");
        students.put(3, "Vishal");
        students.put(4, "Hemant");
        students.put(5, "Narender");
 
        //Print HashMap objects 
        for (Map.Entry entry : students.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
    }  
}

Output

1 - Jai
2 - Mahesh
3 - Vishal
4 - Hemant
5 - Narender

LinkedHashMap:

Lisk HashMap, it also contains the objects in key-value pair form and maintains insertion order for it’s elements. It extends AbstractMap class and implements the NavigableMap interface. It also does not allow duplicate key.

Example

import java.util.*; 
public class Main {  
 
    public static void main(String args[]) 
    { 
        //Create LinkedHashMap Object
        LinkedHashMap<Integer,String> students = new LinkedHashMap<Integer,String>();
 
        //Add objects in LinkedHashMap
        students.put(1, "Jai");
        students.put(4, "Hemant");
        students.put(5, "Narender");
        students.put(2, "Mahesh");
        students.put(3, "Vishal");
 
        //Print LinkedHashMap objects 
        for (Map.Entry entry : students.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
    }  
}

Output

1 - Jai
4 - Hemant
5 - Narender
2 - Mahesh
3 - Vishal

TreeMap:

Lisk HashMap and LinkedHashMap, it also contains the elements/objects in key-value pair form and maintains ascending order for it’s elements. It extends AbstractMap class and implements the NavigableMap interface. It does not allow duplicate key.

Example

import java.util.*; 
public class Main {  
 
    public static void main(String args[]) 
    { 
        //Create TreeMap Object
        TreeMap<Integer,String> students = new TreeMap<Integer,String>();
 
        //Add objects in TreeMap
        students.put(1, "Jai");
        students.put(4, "Hemant");
        students.put(5, "Narender");
        students.put(2, "Mahesh");
        students.put(3, "Vishal");
 
        //Print TreeMap objects 
        for (Map.Entry entry : students.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
    }  
}

Output

1 - Jai
2 - Mahesh
3 - Vishal
4 - Hemant
5 - Narender

HashTable:

Like HashMap, LinkedHashMap, and TreeMap, it also contains items/elements/objects in key-value pair and does not allow duplicate keys. It extends Dictionary class and implements Map, Cloneable, Serializable interface. It uses hashcode() method for finding the position of the objects/elements.

Hashtable is synchronized. It can not contain null key or value.

Example

import java.util.*; 
public class Main {  
 
    public static void main(String args[]) 
    { 
        //Create Hashtable Object
        Hashtable<Integer,String> students = new Hashtable<Integer,String>();
 
        //Add objects in Hashtable
        students.put(1, "Jai");
        students.put(4, "Hemant");
        students.put(5, "Narender");
        students.put(2, "Mahesh");
        students.put(3, "Vishal");
 
        //Print Hashtable objects 
        for (Map.Entry entry : students.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
    }  
}

Output

5 - Narender
4 - Hemant
3 - Vishal
2 - Mahesh
1 - Jai

When to use HashMap, LinkedHashMap, TreeMap, and HashTable in Java

  • Use HashMap: When there is no need to maintain any order of elements and we are working on single threaded environment.
  • Use LinkedHashMap: When there is a need to maintain insertion order of elements and we are working on single threaded environment.
  • Use TreeMap: When there is a need to maintain ascending order of elements and we are working on single threaded environment.
  • Use HashTable: When there is no need to maintain any order of elements and we are working on multi-threaded environment.

Difference between HashMap, LinkedHashMap, TreeMap, and HashTable in Java

HashMap Hashtable LinkedHashMap TreeMap
HashMap does not maintains any order for its objects.

 

Hashtable does not maintains insertion order for its objects. LinkedHashMap maintains insertion order for its objects. TreeMap maintains ascending order for its objects.
HashMap is not Thread-Safe because it is not synchronized.  Its operations are much faster as compared to Hashtable. Hashtable is Thread-Safe as it is synchronized. Its operations are slower as compared to HashMap. LinkedHashMap is not Thread-Safe because it is not synchronized. It is slow as compared to HashMap because it uses Doubly Linked list internally which result into Time and space complexity overhead. TreeMap is also not Thread-Safe because it is not synchronized. It is slow as compared to HashMap and LinkedHashMap because of sorting operations as Comparator will be called for sorting purpose.
HashMap allows to store one null key and many null values i.e. many keys can have null value in java. Hashtable does not allow to store null key or null value.

Any attempt to store null key or value throws runtimeException (NullPointerException) in java.

LinkedHashMap allows to store one null key and many null values i.e. any key can have null value in java. TreeMap does not allow to store null key but allow many null values.

Any attempt to store null key throws runtimeException (NullPointerException) in java.

It implements java.util.Map It implements java.util.Map It implements java.util.Map It implements

java.util.Map

java.util.SortedMap

java.util.NavigableMap

It uses buckets to store the elements. It uses buckets to store the elements. It uses doubly linked lists to store the elements. It uses Red black tree to store the elements.
Order of complexity: O(1) Order of complexity: O(1) Order of complexity: O(1) Order of complexity: O(log(n))
Introduced in JDK 2.0 Introduced in JDK 1.0 Introduced in JDK 4.0 Introduced in JDK 2.0

Java interview questions on collections

Please Share