Introduction

Some of the characteristics of a hashtable are explained below:

  • A hashtable stores {key, value} pair in a hashtable.
  • It is similar to a hashmap the only difference is hashtable is synchronized whereas a hashmap doesn’t.
  • The Hashtable class has a default capacity of 11 whereas its load factor is 0.75.
  • The Hashtable class provides a not fail-fast enumeration while a HashMap doesn’t offer any enumeration.

Java provides us Hashtable class in Java that maps a key to value. A hashtable in Java can be declared using the following syntax:

Syntax:

public class Hashtable<U,V> extends Dictionary<U,V> implements Map<U,V>, Cloneable, Serializable

Parameters:

U: It specifies the type of key of the hashtable
V: It specifies the type of value of the hashtable

Creating a hashtable in Java

The hashtable class is defined under the java.util.Hashtable package. Let us see the working of a hashtable with the help of the following example:

Source Code:

import java.io.*;
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
 
        // Declaring of a Hashtable
        // using Generics
        Hashtable<String, Integer> myHashtable
            = new Hashtable<String, Integer>();
 
        // Putting the elements in hashtable
        // using put() method
        myHashtable.put("Board Infinity", 1);
        myHashtable.put("A", 2);
        myHashtable.put("B", 3);
 
        myHashtable.put("C", 4);
        myHashtable.put("D", 5);
       
 
        // Print the hashtable
        System.out.println("myHashtable: " + myHashtable);
      ;
    }
}

Output:

write your code here: Coding Playground

Methods on hashtable

The hashtable class provides us numerous methods that can be implemented on hashtable. Some of these methods are explained below:

put() method

This method is used to insert elements in a defined hashtable. This function has the following syntax:

Syntax:

hashtable.put(key, value)

Parameters:

key: The corresponding key of the element
value: The corresponding value of the element

Source Code:

import java.io.*;
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
 
        // Declaring of a Hashtable
        // using Generics
        Hashtable<Integer, Integer> myHashtable
            = new Hashtable<Integer, Integer>();
 
        // Putting the elements in hashtable
        // using put() method
        myHashtable.put(1, 10);
        myHashtable.put(2, 20);
        myHashtable.put(3, 30);
 
        myHashtable.put(4, 40);
        myHashtable.put(5, 50);
       
 
        // Print the hashtable
        System.out.println("myHashtable: " + myHashtable);
      ;
    }
}

Output:

remove() method

This method is used to remove an element from a defined hashtable. This function has the following syntax:

Syntax:

hashtable.remove(key)

Parameter:

key: The key of the element to be removed

Now consider a program given below:

Source Code:

import java.io.*;
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
 
        // Declaring of a Hashtable
        // using Generics
        Hashtable<Integer, Integer> myHashtable
            = new Hashtable<Integer, Integer>();
 
        // Putting the elements in hashtable
        // using put() method
        myHashtable.put(1, 10);
        myHashtable.put(2, 20);
        myHashtable.put(3, 30);
 
        myHashtable.put(4, 40);
        myHashtable.put(5, 50);
       
       
        // Print the hashtable
        System.out.println("myHashtable: " + myHashtable);
       
        // Remove the element
        // having key as 1
        myHashtable.remove(1);
 
        // Print the hashtable
        System.out.println("myHashtable: " + myHashtable);

       
    }
}

Output:

write your code here: Coding Playground

Traversing a hashtable

We can use advance for loop to iterate over a hashtable in Java. Consider a program below iterating over a hashtable:

Source Code:

import java.io.*;
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
 
        // Declaring of a Hashtable
        // using Generics
        Hashtable<Integer, Integer> myHashtable
            = new Hashtable<Integer, Integer>();
 
        // Putting the elements in hashtable
        // using put() method
        myHashtable.put(1, 10);
        myHashtable.put(2, 20);
        myHashtable.put(3, 30);
 
        myHashtable.put(4, 40);
        myHashtable.put(5, 50);
       
       
          // Iterating using advance for loop
        for (Map.Entry<Integer, Integer> currentElement : myHashtable.entrySet())
            System.out.println(currentElement.getKey() + " "
                              + currentElement.getValue());

       
    }
}

Output:

write your code here: Coding Playground

Internal working of a hashtable

A hashtable datastructure consist of array of buckets that stores key and value pairs inside them. In order to determine which bucket should a ke/value map, it makes the use of hashCode() method.

Internally, a hash function is also used to determine the location for a given key in the list of buckets. Most of the time, a hashcode is a non-negative integer that is surely equal for equal objects but may or may not be equal for unequal objects. Internally, to check whether two object are equal or not, hashtable makes the use of equals() method.

Conclusion

In this article, we discussed about what are hashtables, hashtable methods, hashtables internal working. I hope this article will help to strengthen your knowledge in the field of Java.