HashMap vs HashSet vs Hashtable – HashMap, HashTable, TreeMap and LinkedHashMap with Examples:
Collections:
HashMap, HashSet and Hashtable are a part of Collections. (HashSet 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)

We can see there are 4 types under Map (from the diagram above)

  • HashMap
  • Hashtable
  • TreeMap
  • LinkedHashMap

HashMap:
HashMap (Key, Value) pairs. HashMap is Unsynchronized and accepts null key and value pairs. HashMap can have only one null key, However if you pass another null key with different value, the value at null key will get replaced by new value.

Hashtable:
Hashtable is similar to HashMap except, Hashtable is Synchronized and does not accept null key value pairs.

TreeMap:
TreeMap is NavigableMap implementation, It is sorted according to order of keys.

LinkedHashMap:
LinkedHashMap stores the key value pair by insertion order.

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
import java.util.LinkedHashMap;

        

/**
 *
 * @author Vikram
 */
public class MapPerformance {
public static void main(String args[]){
        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
        TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
        LinkedHashMap<Integer, String> linkedHashMap = new LinkedHashMap<Integer, String>();
        Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
        hashMap.put(1, "Alpha");
        hashMap.put(3,"Charlie");
        hashMap.put(2,"Beta");
       
        
        hashTable.put(1, "Delta");
        hashTable.put(3,"Foxy");
        hashTable.put(2,"Eagle");
        
        
        linkedHashMap.put(1, "Gamma");
        linkedHashMap.put(3,"Hexa");
        linkedHashMap.put(2,"Infinity");
        
        
        treeMap.put(1, "Jax");
        treeMap.put(3,"Lotus");
        treeMap.put(2,"Kite");
        
        
        System.out.println("--------------HashMap Entries-------------");
        
        for(Map.Entry<Integer, String> hashMapEntry: hashMap.entrySet()){
            System.out.println("Key in HashMap: "+hashMapEntry.getKey());
            System.out.println("Value in HashMap: "+hashMapEntry.getValue());
            
        }
        
        System.out.println("--------------HashTable Entries-------------");
        
        for(Map.Entry<Integer, String> hashTableEntry: hashTable.entrySet()){
            System.out.println("Key in HashTable: "+hashTableEntry.getKey());
            System.out.println("Value in HashTable: "+hashTableEntry.getValue());
            
        }
        
        System.out.println("--------------LinkedHashMap Entries-------------");
        
        for(Map.Entry<Integer, String> linkedHashMapEntry: linkedHashMap.entrySet()){
            System.out.println("Key in HashTable: "+linkedHashMapEntry.getKey());
            System.out.println("Value in HashTable: "+linkedHashMapEntry.getValue());
            
        }
        
        System.out.println("--------------TreeMap Entries-------------");
        
        for(Map.Entry<Integer, String> treeMapEntry: treeMap.entrySet()){
            System.out.println("Key in HashTable: "+treeMapEntry.getKey());
            System.out.println("Value in HashTable: "+treeMapEntry.getValue());
            
        }
        
        System.out.println("--------------Searching Key-------------");
        
        for(Map.Entry<Integer, String> hashMapEntry: hashMap.entrySet()){
            if(hashMapEntry.getKey().equals(2)){
                System.out.println("Match Found");   
            }
        }
        
         System.out.println("--------------HashTable Entries After Removal-------------");
         hashMap.remove(2);
        
        for(Map.Entry<Integer, String> hashMapEntry: hashMap.entrySet()){
            System.out.println("Key in HashMap: "+hashMapEntry.getKey());
            System.out.println("Value in HashMap: "+hashMapEntry.getValue());  
        }
        
        System.out.println("--------------Getting value for the Key-------------");
        //Get Value for Specific Key
        String value = hashMap.get(3);
        System.out.println("Value for the Key 3: "+value);
    } 
}

In the above example, We have used HashMap, Hashtable, TreeMap and LinkedHashMap, Now let us see the output of this code. We will be able to understand the sorting order how it gets sorted from output.

op1
op2
From the output, We can see that Hashtable orders on descending order, TreeMap sorts on natural key ordering and LinkedHashMap sortes as per insertion order.

By Sri

One thought on “HashMap vs HashSet vs Hashtable – HashMap, HashTable, TreeMap and LinkedHashMap with Examples”

Leave a Reply

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