- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between HashMap and HashSet in Java.
HashMap and HashSet both are one of the most important classes of Java Collection framework.
Following are the important differences between HashMap and HashSet.
Sr. No. | Key | HashMap | HashSet |
---|---|---|---|
1 | Implementation | Hashmap is the implementation of Map interface. | Hashset on other hand is the implementation of set interface. |
2 | Internal implementation | Hashmap internally do not implements hashset or any set for its implementation. | Hashset internally uses Hashmap for its implementation. |
3 | Storage of elements | HashMap Stores elements in form of key-value pair i.e each element has its corresponding key which is required for its retrieval during iteration. | HashSet stores only objects no such key value pairs maintained. |
4 | Method to add element | Put method of hash map is used to add element in hashmap. | On other hand add method of hashset is used to add element in hashset. |
5 | Index performance | Hashmap due to its unique key is faster in retrieval of element during its iteration. | HashSet is completely based on object so compared to hashmap is slower. |
6 | Null Allowed | Single null key and any number of null value can be inserted in hashmap without any restriction. | On other hand Hashset allows only one null value in its collection,after which no null value is allowed to be added. |
Example of Hashmap vs Hashset
JavaTester.java
import java.util.HashSet; public class JavaTester { public static void main(String[] args){ HashSet<String> hs = new HashSet<String>(); hs.add("John"); hs.add("Smith"); hs.add("Peter"); System.out.println("Before adding duplicate values
" + hs); hs.add("John"); hs.add("Smith"); System.out.println("
After adding duplicate values
" + hs); hs.add(null); hs.add(null); System.out.println("
After adding null values
" + hs); } }
Output
Before adding duplicate values [John, Smith, Peter] After adding duplicate values [John, Smith, Peter] After adding null values [null, John, Smith, Peter]
Example
JavaTester.java
import java.util.HashMap; public class JavaTester { public static void main(String[] args){ HashMap<Integer, String> hm = new HashMap<Integer, String>(); hm.put(12, "John"); hm.put(2, "Smith"); hm.put(7, "Peter"); System.out.println("
HashMap object output :
" + hm); hm.put(12, "Smith"); System.out.println("
After inserting duplicate key :
" + hm); } }
Output
HashMap object output : {2=Smith, 7=Peter, 12=John} After inserting duplicate key : {2=Smith, 7=Peter, 12=John}
- Related Articles
- Difference between ArrayList and HashSet in Java
- Difference between HashMap and HashTable in Java.
- Difference between HashMap and ConcurrentHashMap in Java
- Difference between HashTable and HashMap in Java
- Difference between EnumMap and HashMap in Java
- Difference Between HashMap and TreeMap in Java
- Difference Between HashMap and LinkedHashMap in Java
- Difference between TreeMap, HashMap, and LinkedHashMap in Java
- Difference between TreeMap, HashMap and LinkedHashMap in Java programming
- Difference between Concurrent hash map and Synchronized hashmap in Java
- Difference between HashMap and ConcurrentHashMap
- Difference between Map and HashMap
- Differences between HashMap and Hashtable in Java
- Differences between TreeMap, HashMap and LinkedHashMap in Java
- What is the differences between HashMap and HashTable in Java

Advertisements