Let's learn java programming language with easy steps

Thursday, 31 August 2017

Difference Between HashSet and HashMap in Java

HashSet vs HashMap

HashSet vs HashMap

Here we will discuss, what is the difference between HashSet and HashMap in java. This is really very nice and useful and mostly asked java interview question.

In the last post, we have discussed that what is the difference between HashMap and Hashtable class in java collection framework but here we will learn only what is the difference between HashMap and HashSet in java with example.


Java HashSet

  • HashSet class implements the Set interface.
  • HashSet internally uses HashMap to store it elements.
  • HashSet class doesn't contain duplicate elements i.e it stores only unique elements.
  • In HashSet, There is no key-value combination for storing data. We can insert only elements or values.
  • According to some developers, Java HashSet performance is slower than HashMap.
  • HashSet uses add(object) method to store the elements.
  • There are only one null value allow in hash set.


HashSet Example in Java

This is the simple java HashSet example where we will insert some elements and iterate all the elements one by one.

import java.util.*;
class Demo
{
public static void main(String args[])
{
//declaring hash set
Set<String> hs = new HashSet<String>();
hs.add("java");//inserting elements
hs.add("c++");
hs.add("c#");
hs.add("html");
hs.add("css");
hs.add("java");
hs.add(null);

//iterating one by one
Iterator i = hs.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output: c#
             null
             c++
             html
             java
             css


Java HashMap

  • HashMap class implements Map interface.
  • HashMap class internally uses array of Entry<k,v> to store the elements.
  • HashMap class doesn't allow duplicate key but it allows duplicate values.
  • HashMap uses key-value combination for storing the data.
  • HashMap performance is faster than HashSet in java.
  • HashMap uses put(key, value) method to store the data.
  • There can be one null key and multiple null values in hash map.


HashMap Example in Java


This is the second example in HashSet vs HashMap java. Java HashMap store the data on the basis of key and value pairs.


import java.util.*;
class Simple
{
public static void main(String args[])
{
//creating HashMap

HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(101, "Ram");
hm.put(102, "Ram");
hm.put(103, "Suman");
hm.put(103, "Kiran");
hm.put(104, "Sekhar");
hm.put(105, "Vimal");

for(Map.Entry m : hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output:  101 Ram
              102 Ram
              103 Kiran
              104 Sekhar
              105 Vimal

In the above example, There can be duplicate values in hash map but there cannot be duplicate key in hash map class in java. It overrides duplicate keys.


Similarities Between HashSet and HashMap class in Java

There are some similarities between these two classes in collection framework. These are...

HashSet Class: - HashSet class are not synchronized i.e it is not thread-safe. There is no guarantee to maintain the insertion order of an element. It provides fast performance like HashMap but HashMap is much faster than hash set.

HashMap Class: HashMap class are not synchronized like hash set class.
HashMap also does not maintain insertion order of an element like hash set class. Both hash set and hash map provide fast performance.

Read More:

Difference Between ArrayList and LinkedList in Java.
Difference Between ArrayList and Vector in Java.
Difference Between HashMap and Hashtable.
Difference Between Abstract Class and Interface in Java.
Difference Between Array and Collection.
Java Collection Interview Questions.

I hope, the difference between HashSet and HashMap in java post will help you to handle java related interviews specially in HashSet vs HashMap.

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate