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,

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.Set;
import java.util.Collections;

 * @author Vikram
public class MapPerformance {
public static void main(String args[]){
        String search = "Beta"; 
        HashSet<String> hashSet = new HashSet<String>();
        TreeSet<String> treeSet = new TreeSet<String>();
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
        // Adding elements in Sets
        System.out.println("HashSet elements: "+hashSet);
        System.out.println("TreeSet elements: "+treeSet);
        System.out.println("LinkedHashSet elements: "+linkedHashSet);
        // Iterator using Set
        System.out.println("--------------------Iterating in Set--------------------------");
        Iterator<String> itr = hashSet.iterator();
            System.out.println("HashSet Iterator: "+itr.next());
        //Searching inside Set
        for(String searching: hashSet){
                System.out.println("Match Found");
        //Searching inside Set
        System.out.println("---------------------Searching using Contains-------------------------");
        if(hashSet.contains(new String("Beta"))){
            System.out.println("Match is found using Contains");
        System.out.println("Removing Beta");
         System.out.println("--------------------After Removing--------------------------");
        Iterator<String> itr1 = hashSet.iterator();
            System.out.println("HashSet Iterator: "+itr1.next());
        // Synchronizing hashSet
        Set<String> hashSetSync = Collections.synchronizedSet(hashSet); 

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


By Sri

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 *