HashMap vs HashSet vs Hashtable – HashSet with Examples

HashMap vs HashSet vs Hashtable – HashSet with Examples:
HashMap, HashSet and Hashtable are a part of Collections. (HashMap Here)





HashMap, HashSet and HashTable:

HashMap, HashSet and Hashtable usually store values in key value pair. In this article we are going to understand in detail regarding HashMap, HashSet and HashTable

HashMap HashSet Hashtable
It allows null for both key and values HashSet permits to have a single null value It does not allow null for both key and value
HashMap does not maintain any order HashSet does nto maintain any insertion order, cause insertion order is not constant overtime. But if we use LinkedHashSet it maintains an order Hashtable does not maintain insertion order
HashMap uses put method to insert into hashmap HashSet uses add method to insert into hashset HashTable uses put method to insert into hashtable
HashMap is not Synchronized, better performance HashSet is not Synchronized but can be synchronized externally HashTable is Synchronized (thread safe)

The differences between hashmap vs hashtable vs hashset is important for interview purposes.

HashSet has 3 Subtypes,

  • HashSet
  • TreeSet
  • LinkedHashSet


HashSet TreeSet LinkedHashSet
HashSet is fastest than LinkedHashSet and TreeSet TreeSet is slow when compared with both Hashset and LinkedHashSet LinkedHashSet is second fastest next to HashSet
HashSet does not maintain any order TreeSet maintains Sorting Order LinkedHashSet maintains insertion order
HashSet allows null TreeSet does not allow null LinkedHashSet allows null
HashSet uses equals() method TreeSet uses compareTo() method LinkedHashSet uses equals() method
HashSet backed by HashMap TreeSet backed by NavigableMap LinkedHashSet backed by HashSet

Which Set to use and When:
If we need to perform operations faster in Set, We need to use HashSet. If we need elements in inserted order we use LinkedHashSet and if we need elements in sorted order we use TreeSet.

How to Make set Synchronized Externally:
Set set = Collections.synchronizedSet(new HashSet());

Now let use see an example (CRUD) operations in Set,

In the above example we have used HashSet. Same way these operations can be applied for TreeSet and LinkedHashSet.


2 thoughts on “HashMap vs HashSet vs Hashtable – HashSet with Examples

Leave a Reply

Your email address will not be published. Required fields are marked *