Java Hashtable is a legacy implementation class of the Map interface and an implementation of hash table data structure.  It inherits Dictionary class and implements the Map interface.

1. Characteristics of Java Hashtable

  1. Hashtable class implements the java.util.Map interface, the under lying data structure is hash table.
  2. Insertion order is not preserved and it is based on hash code of the keys.
  3. Null key (or) null value is not allowed otherwise we will get NullPointerException.
  4. Duplicate keys are not allowed but values can be duplicated. To follow this, the objects used as keys must implement the hashCode method and the equals method.
  5. Java Hashtable methods are synchronized.

2. When to use Hashtable

The general purpose of map in java is to hold key value pairs, for example if we wants to hold a student roll number and and his name (roll number is a key as it is unique and name can be value). Either Hashtable or HashMap having hash table implementation is underlying data structure. The key difference between Hashtable and HashMap is synchronization. All the Hashtable methods are synchronized in java, hence Hashtable operations are thread safe. HashMap operations are relatively faster than Hashtable operations if we considered multiple threads.

3. How to create Hashtable

Below are the constructors java Hashtable provides to create Hashtable insatnces.

Constructors:

  • Hashtable():  Constructs a new, empty hashtable with a default initial capacity 11.
  • Hashtable(int size) :  Constructs a new, empty hashtable with the specified initial capacity and default load factor (0.75).
  • Hashtable(int size, float fillRatio) : Constructs a new, empty hashtable with the specified initial capacity and the specified load factor.
  • Hashtable(Map m) :  Constructs a new hashtable with the same mappings as the given Map (useful to convert other map implementation to Hastable).
Add RollNos to Hashtable.

 

4. How Hashtable works in java

When you create a new hastable instance, a new empty hashtable will be created with initial capacity 11. Hashtable uses an array and each position in the arrays is a bucket which can. The formula calculates an index for the key is:

int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;

Below image illustrates how hashtable stores key value pairs based on hash code and how it get index. To understand little deeper, in this example we have created a RollNo class to use roll number as key and hashcode() method overridden in RollNo class, so that we know that exactly what is the hash code of a key, in this example the key itself is hashcode.

java hashtable data structure

4.1. Collisions

If you notice at Index 5, two key value pairs stored in that bucket, even different hash codes generates the same index. A single bucket stores multiple entries, we refer to this as a collision. Which must be searched sequentially, to resolve collisions Hashtable stores key value pairs in a linked list in each bucket, this is called chaining.

4.2. rehash() in Hashtable

By default the load factor is 0.75 in Hashtable, array capacity doubles when 3/4 th of buckets not empty in Hashtable. This operation is executed by rehash() method in Hashtable class.

For Example, if the 8 (3/4 th of initial capacity ) buckets are filled in the array, the new capacity become 23.

int newCapacity = (oldCapacity << 1) + 1;

Below image illustrates that how rehashing works in java Hashtable.

Java hashtable rehash example

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method., should follow the hashcode() contract.

5. Java Hashtable methods

All the Hashtable methods are works same as HashMap , the key difference is all the Hashtable methods are synchronized. For the code examples of Hashtable refer to HashMap.

6. How to iterate Java Hashtable

There are different approaches to iterate java Hashtable, all of them explained in iterate java map article. They are common to iterate any map implementation class in java.

Conclusion

In this article we have covered what is java Hashtable class, its features, how internally Hashtable works, how rehashing works and the importance of hashcode() method and about collisions in java Hashtable.