Home » Java programming language

Differences between HashMap and TreeMap in Java

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

HashMap and TreeMap in Java

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

TreeMap

  • This class is available in java.util package.
  • This class is an implementation class of Map interface.
  • The underlying data structure of TreeMap is RED-BLACK Tree.
  • In TreeMap "insertion order of the elements" is not preserved because elements will be inserted according to some sorting order of keys (Here sorting will be done based on keys).
  • In TreeMap object is represented as a group of elements as a key-value pair.
  • In TreeMap "duplicates insertion is not possible" for keys (i.e. it is not allowed to insert duplicate elements for keys).
  • In TreeMap "duplicates insertion is possible" for values (i.e. it is allowed to insert duplicate elements for values or there are no restrictions on values).
  • In TreeMap sorting will be done of two types:
    1. Default Natural Sorting (Ascending order)
    2. Customized Sorting(Either Ascending or Descending)
  • In Default natural sorting "TreeMap keys" should be homogenous and Comparable.
  • In Customized sorting "TreeMap keys" need not be homogenous and Comparable.
  • In TreeMap "null insertion is possible" for keys as the first elements or in other words if we will insert null after the first element then we will get an exception.
  • In TreeMap "null insertion is not possible" for keys for non-empty Map.

Example:

// Java program to demonstrate the behavior of TreeMap 

import java.util.*;

class TreeMapClass {
    public static void main(String[] args) {
        // Creating an instance of TreeMap
        TreeMap tm = new TreeMap();

        // By using put() to add elements in TreeMap
        tm.put(1, "Java");
        tm.put(3, "C");
        tm.put(2, "C++");
        tm.put(4, "Java");
        tm.put(6, null);
        tm.put(7, 10);
        tm.put(2, "Ruby");

        /*  tm.put("Java" , "is a programming"); 
            Here hetrogenous object is not allowed for keys */

        /*  tm.put(null , "Python"); 
            Here null insertion for keys is not 
            possible for non-empty TreeMap */

        /*  tm.put(2 , "Ruby"); 
            Here we will not get any exception but 
            only one will be considerable */

        // Display Current TreeMap
        System.out.println("Display Current TreeMap is :" + tm);
    }
}

Output

E:\Programs>javac TreeMapClass.java

E:\Programs>java TreeMapClass
Display Current TreeMap is :{1=Java, 2=Ruby, 3=C, 4=Java, 6=null, 7=10}

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

HashMap

  • This class is available in java.util package.
  • This class is an implementation class of Map interface.
  • The underlying data structure of HashMap is Hashtable.
  • HashMap is a parent of LinkedHashMap.
  • In HashMap "insertion order of the elements" is not preserved because elements will be inserted according to some hashCode of keys (i.e. insertion order is not needed to be same as the retrieval order).
  • In HashMap object is represented as a group of elements as a key-value pair.
  • In HashMap "duplicates insertion is not possible" for keys (i.e. it is not allowed to insert duplicate elements for keys).
  • In HashMap "duplicates insertion is possible" for values (i.e. it is allowed to insert duplicate elements for values or there is no restrictions on values).
  • In HashMap "null insertion is possible" for keys and values but once for keys and multiple for values.
  • In HashMap "Heterogenous objects" are allowed for both keys and values.

Example:

// 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);

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

        // 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=null, Java=1000}
Current HashMap Key values is :[4000, 2000, null, 1000, 3000, null, 1000].



Comments and Discussions!

Load comments ↻






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