Easy Tutorial
For Competitive Exams

Java Programming HashTable

What is Hashtable?

Hashtable class implements a hashtable, which maps keys to values.

Any non-null object can be used as a key or as a value.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

How to perform Basic Operations in Hashtable?

The Basic operations are:

  • creating hashtable object
  • adding key-value pair
  • getting the value based on key
  • checking hashtable is empty or not
  • removing an element
  • size of the hashtable

Basic Operations

import java.util.Hashtable;
  public class Basic {
      public static void main(String a[]){
        Hashtable ht = new Hashtable();
        ht.put("first", "FIRST INSERTED");
        ht.put("second", "SECOND INSERTED");
        ht.put("third","THIRD INSERTED");
        System.out.println(ht);
        System.out.println("Value of key 'second': "+ht.get("second"));
        System.out.println("Is Hashtable empty? "+ht.isEmpty());
        ht.remove("third");
        System.out.println(ht);
        System.out.println("Size of the Hashtable: "+ht.size());
    }
}
Try it Yourself

How to iterate through Hashtable?

We can iterate through each and every element by getting all keys as set object.

Using each element as a key , we can get values from Hashtable.

Example

import java.util.HashMap;
import java.util.Hashtable;
public class Read {
   public static void main(String a[]){
         Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println("Value of "+key+" is: "+hm.get(key));
        }
    }
}
Try it Yourself

How to copy Map content to another Hashtable?

putAll() method is used to copy another collection object.

Example

import java.util.HashMap;
import java.util.Hashtable;
 public class Copy {
    public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        HashMap subMap = new HashMap();
        subMap.put("s1", "S1 VALUE");
        subMap.put("s2", "S2 VALUE");
        hm.putAll(subMap);
        System.out.println(hm);
    }
}
Try it Yourself

How to search a key in Hashtable?

The method containsKey() is used to find whether the specified key exists or not.

Example

import java.util.Hashtable;
public class KeySearch {
    public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsKey("first")){
            System.out.println("The Hashtable contains key first");
        } else {
            System.out.println("The Hashtable does not contains key first");
        }
        if(hm.containsKey("fifth")){
            System.out.println("The Hashtable contains key fifth");
        } else {
            System.out.println("The Hashtable does not contains key fifth");
        }
    }
}
Try it Yourself

How to search a value in Hashtable?

The method containsValue() helps us to find whether the specified value exists or not.

Example

import java.util.Hashtable;
public class ValueSearch {
    public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        if(hm.containsValue("SECOND INSERTED")){
            System.out.println("The Hashtable contains value SECOND INSERTED");
        } else {
            System.out.println("The Hashtable does not contains value SECOND INSERTED");
        }
        if(hm.containsValue("first")){
            System.out.println("The Hashtable contains value first");
        } else {
            System.out.println("The Hashtable does not contains value first");
        }
    }
}
Try it Yourself

How to get all keys from Hashtable?

keySet() method returns Set object will all keys.

Example

import java.util.Hashtable;
import java.util.Set;
public class tableKeys {
    public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys){
            System.out.println(key);
        }
    }
}
Try it Yourself

How to get entry set from Hashtable?

Entry object provides getter methods to access key-value objects.

entrySet() method returns Set object with Entry values.

Example

import java.util.Hashtable;
import java.util.Set;
import java.util.Map.Entry;
public class EntrySet {
     public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println(hm);
        Set> entires = hm.entrySet();
        for(Entry ent:entires){
            System.out.println(ent.getKey()+" => "+ent.getValue());
        }
    }
}
Try it Yourself

How to delete all elements from Hashtable?

By calling clear() method, we can remove all elements from Hashtable at once.

Example

import java.util.Hashtable;
public class Clear {
     public static void main(String a[]){
        Hashtable hm = new Hashtable();
        hm.put("first", "FIRST INSERTED");
        hm.put("second", "SECOND INSERTED");
        hm.put("third","THIRD INSERTED");
        System.out.println("My Hashtable content:");
        System.out.println(hm);
        System.out.println("Clearing Hashtable:");
        hm.clear();
        System.out.println("Content After clear:");
        System.out.println(hm);
    }
}
Try it Yourself

Solve this!!

#20976,20977,20978
Share with Friends