0% found this document useful (0 votes)
99 views

5 Difference Between Hashmap and Hashset in Java With Example

Hash Map
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

5 Difference Between Hashmap and Hashset in Java With Example

Hash Map
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

12.11.

2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

JAVA HUNGRY Search this blog... Search

Java developers tutorials and coding.

HOME STRING COLLECTIONS INTERVIEW INTERVIEW TIPS

DATA-STRUCTURES

SCJP THREADS BEST JAVA BOOKS JAVA CODING PROGRAM MORE JAVA TOPICS

5 Difference Between
HashMap And
HashSet In Java With
Example

We have already discussed the


difference between HashMap and
Hashtable . Now in this post you will
understand the difference between
HashMap and HashSet classes . We
will also look into the HashMap and
HashSet example. We have already
discussed HashMap internal WHATS HOT
mechanism , HashSet internal
working in java . Difference between Arraylist and Vector :
Core Java Interview Collection Question

Read Also : Frequently asked Java


Best Books for Learning Java
Collections Interview Questions

Amazon Interview Question : First Non

https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 1/6
12.11.2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

Difference between HashMap and repeated character in String


HashSet
Count total number of times each alphabet
1. Duplicates : HashSet does not
appears in the string java program code with
allow duplicate values , in other
example
words, adding a duplicate value
leaves the HashSet object
unchanged. Java 8 new features : Lambda expressions ,
optional class , Defender methods with
If the HashMap previously contain the examples
mapping for the key, the old value is
replaced. HashMap does not allow
duplicate keys. In other words,
HashMap duplicate values can exist
in HashMap.

2. Dummy value : There is no


concept of dummy value in HashMap
.

HashSet internally uses HashMap to


add elements. In HashSet , the
argument passed in add(Object)
method serves as key K . we need to
associate dummy value (automatically
created by jdk) for each value passed
in add(Object) method.

3. Implementation : HashMap
implements Map interface, while
HashSet implements Set interface.

4. Number of objects during


add(put) operation : HashMap
requires two objects put(K key , V
Value) to add an element to HashMap
object.
HashSet requires only one object
add(Object o) .
Subscribe for Our Newsletter

5. Adding or Storing mechanism


: HashMap internally uses hashing to
add or store objects. HashSet
internally uses HashMap object to
add or store the objects.

https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 2/6
12.11.2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

Example of HashMap and HashSet


in Java

import java.util.HashMap;
import java.util.HashSet;

public class HashMapHashSetExa

public static void main(String[] a

HashSet<String> hashsetobj = n
hashsetobj.add("Alive is awesom
hashsetobj.add("Love yourself")
System.out.println("HashSet obj

HashMap hashmapobj = new H POPULAR POSTS


hashmapobj.put("Alive is ", "awe
hashmapobj.put("Love", "yourse Java Interview Questions
System.out.println("HashMap ob
Top 50 Java Collections Interview Questions
} and Answers
}
Web Services Interview Questions

Java Multithreading Interview Questions and


Answers

Java Interview Programs


Output : HashSet object output :
{Love yourself, Alive is awesome}
HashMap object output :
{Alive is =awesome,
Love=yourself}

Similarities between HashMap and


HashSet
https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 3/6
12.11.2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

1. Performance : Performance of
add,put,remove operations are
almost same i.e constant time for
HashSet and HashMap. It is
incorrect to say HashMap is faster
than HashSet.

2. Insertion Order : Both HashMap


and HashSet does not guarantee that
the order will remain constant over
time.

3. Unsynchronized : Both HashMap


and HashSet implementation is
unsynchronized.

4. Iterator type : Iterator's returned


by both (HashMap and HashSet)
class's iterator and collection view
methods are fail-fast. find detailed
explanation here fail-safe vs fail-fast
in java.

When to use HashMap and


HashSet in Java ?

The only time we need to prefer


HashSet over HashMap when we are
asked to maintain uniqueness in our
collection object. Otherwise , always
prefer HashMap over HashSet as
HashSet is just a wrapper around
HashMap.
https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 4/6
12.11.2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

Recap : Difference between


HashMap and HashSet in Java

HashSet
HashMap

Yes,
Duplicate
values No
Duplicates
but no
duplicate
keys

Adding or HashMap
Hashing
Storing object
technique
mechanism

Dummy
No Yes
values

Implements Map Set


Number Of
Objects
required 2 1
during add
operation

In case you have any other query or


doubts regarding difference between
HashMap and HashSet in Java then
please mention in the comments .

Gefällt mir 0 Tweet Share


Share

About The Author

https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 5/6
12.11.2018 5 Difference between HashMap and HashSet in java with example | Java Hungry

Subham Mittal has worked in Oracle for 3 years .


For more java articles ,Click here to Subscribe
JavaHungry

You might also like


6 Difference between Method Overloading and
Method Overriding in Java | Java Hungry
Top 50 Java Collections Interview Questions and
Answers | Java Hungry
2 ways on How to Initialize an Arraylist in java
with Example | Java Hungry
How to Convert Array to TreeSet in Java | Java
Hungry

Recommended by

Comments Community 
1 Login

 Recommend 2 t Tweet f Share

Sort by Best

Join the discussion…

LOG IN WITH

OR SIGN UP WITH DISQUS ?

Name

ihor • 2 years ago

Return to Top ↑ Java Hungry Copyright © 2018 · All Rights Reserved. Designed by studiopress

-->

https://javahungry.blogspot.com/2015/06/difference-between-hashset-and-hashmap-in-java-with-example.html 6/6

You might also like