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

collections

map

 

 

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
        
        hashSet.add("Alpha");
        hashSet.add("Charlie");
        hashSet.add("Beta");
        
        treeSet.add("Alpha");
        treeSet.add("Charlie");
        treeSet.add("Beta");
        
        linkedHashSet.add("Alpha");
        linkedHashSet.add("Charlie");
        linkedHashSet.add("Beta");
        
        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();
        
        while(itr.hasNext()){
            System.out.println("HashSet Iterator: "+itr.next());
        }
        
        //Searching inside Set
        
        System.out.println("---------------------Searching-------------------------");
        
        for(String searching: hashSet){
            if(searching.equals(search)){
                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");
        }
        
        //Removing
        
        System.out.println("---------------------Removing-------------------------");
        System.out.println("Removing Beta");
         hashSet.remove("Beta");
        
         System.out.println("--------------------After Removing--------------------------");
       
        Iterator<String> itr1 = hashSet.iterator();
        while(itr1.hasNext()){
            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.
Output:
output

 

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 *