Home » Java programming language

Differences between HashSet and HashMap class in Java

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

HashMap vs HashSet

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

HashMap

  • This class is available in java.util package.
  • This class is an implementation class of Map interface.
  • HashMap is a parent class of LinkedHashMap.
  • The underlying data structure of HashMap is Hashtable.
  • In HashMap "insertion order is not preserved" because it is based on HashCode of keys(i.e. the insertion order is not needed to be same as the retrieval order).
  • In HashMap object is represented in the form of keys and values where "duplicate keys are not allowed" but the " duplicate values are allowed".
  • In HashMap null can be inserted for both keys and values but we can insert "null" once for keys and we can insert "null" multiple times for values.
  • In HashMap heterogenous object are allowed for both keys and values.

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} 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 {Ruby=4000, C=2000, Django=null, Python=1000, C++=3000, null=null, 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);
        // 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].

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

HashSet

  • This class is available in java.util package.
  • This class is an implementation class of Set interface.
  • HashSet is a parent class of LinkedHashSet.
  • The underlying data structure of HashSet is Hashtable.
  • In HashSet "insertion order is not preserved" (i.e. the insertion order is not needed to be same as the retrieval order).
  • In HashSet "duplicate values are not allowed".
  • In HashSet null can be inserted for values.
  • In HashSet heterogenous object are allowed.

Example:

Let suppose we have a HashSet with few elements. Here we are adding the elements in the order is [1000, 2000, 3000, 4000, null] 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 [null, 1000, 2000, 3000, 4000]

import java.util.*;

class HashSetClass {
    public static void main(String[] args) {
        // Creating an instance of HashSet
        HashSet hs = new HashSet();

        //By using add() method to add some values in HashSet
        hs.add(1000);
        hs.add(2000);
        hs.add(3000);
        hs.add(4000);
        hs.add(null);
        // hs.add(2000); here we will not get any error or exception 
        // it will be ignored

        // Display retrieval order of HashSet
        System.out.println("Current HashSet list is :" + hs);
    }
}

Output

E:\Programs>javac HashSetClass.java

E:\Programs>java HashSetClass
Current HashSet list is :[null, 1000, 2000, 3000, 4000]



Comments and Discussions!

Load comments ↻






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