HashMap and Hashtable stores values in key-value pair. HashSet contains unique elements and HashMap, HashTable contains unique keys. Having these similarities they have some differences also.
HashSet:
HashSet inherits AbstractSet class and implements Set interface. Set objects are always unique and no duplicate objects are allowed. One null key value is allowed. The hashing mechanism is used to insert the objects into a HashSet.
Example
import java.util.*; public class Main { public static void main(String args[]) { //Creating HashSet Object HashSet angularDevepolers = new HashSet(); //Adding objects in HashSet angularDevepolers.add("Navdeep"); angularDevepolers.add("Anil"); angularDevepolers.add("Lokesh"); angularDevepolers.add("Sushil"); angularDevepolers.add("Amrita"); //Printing HashSet System.out.println(angularDevepolers); } } |
Output
[Amrita, Sushil, Navdeep, Lokesh, Anil] |
HashMap:
HashMap class in java, implements the map interface by using a HashTable. It inherits AbstractMap class and implements the Map interface. It represents a group of objects and every object will be in key-value pair form. It maintains no order for its elements. Duplicate key is not allowed. It can have only one null as key and multiple null as values.
Example
import java.util.*; public class Main { public static void main(String args[]) { //Creating HashMap Object HashMap<Integer,String> angularDevepolers = new HashMap<Integer,String>(); //Adding objects in HashMap angularDevepolers.put(1, "Navdeep"); angularDevepolers.put(4, "Anil"); angularDevepolers.put(5, "Lokesh"); angularDevepolers.put(2, "Sushil"); angularDevepolers.put(3, "Amrita"); //Printing HashMap objects for (Map.Entry entry : angularDevepolers.entrySet()) { System.out.println(entry.getKey() + " - " + entry.getValue()); } } } |
Output
1 - Navdeep 2 - Sushil 3 - Amrita 4 - Anil 5 - Lokesh |
Hashtable:
Hashtable inherits Dictionary class and implements Map interface. Hashtable contains elements/objects/items in key-value pair and does not allow any duplicate key. It is Thread-Safe because of its synchronized nature. The null is not allowed for both key and value. The hashcode() method is used to find the position of the elements.
Example
import java.util.*; public class Main { public static void main(String args[]) { //Creating Hashtable Object Hashtable<Integer,String> angularDevepolers = new Hashtable<Integer,String>(); //Adding objects in Hashtable angularDevepolers.put(1, "Navdeep"); angularDevepolers.put(4, "Anil"); angularDevepolers.put(5, "Lokesh"); angularDevepolers.put(2, "Sushil"); angularDevepolers.put(3, "Amrita"); //Printing Hashtable objects for (Map.Entry entry : angularDevepolers.entrySet()) { System.out.println(entry.getKey() + " - " + entry.getValue()); } } } |
Output
5 - Lokesh 4 - Anil 3 - Amrita 2 - Sushil 1 - Navdeep |
Difference between HashSet, HashMap, and HashTable in java
HashMap | HashSet | Hashtable |
---|---|---|
It allows one null for key and multiple null for values | It can have a single null value. | It does not allow null for key as well as for value. |
It does not maintain any order among its objects. | It does not maintain any order among its objects. | It does not maintain any order among its objects. |
It uses put method to insert a new element. | It uses add method to insert a new element. | It uses put method to insert a new element. |
It is not Thread-Safe because it is not Synchronized but it gives better performance. | Like HashMap, it is not Thread-Safe because it is not Synchronized. | It is Thread-Safe because it is Synchronized. |
Java interview questions on collections
- What is the difference between arraylist and vector in java?
- What is the difference between arraylist and linkedlist?
- What is the difference between Iterator and ListIterator?
- What is the difference between Iterator and Enumeration?
- what is the difference between list and set in java?
- what is the difference between set and map in java?
- what is the difference between hashset and treeset in java?
- what is the difference between hashset and hashmap in java?
- what is the difference between hashmap and treemap in java?
- what is the difference between hashmap and hashtable in java?
- what is the difference between collection and collections in java?
- what is the difference between comparable and comparator interfaces?
- what is the hashcode method in java?
- Java equals method
- Java hashCode method
- Why to override hashcode and equals method in java?
- How hashmap works intrnally?
- How put and get works in hashmap?
- How to resolve collision in hashmap?
- How hashmap stores null key?
- How hashset works intrnally?