Java Hashtable with Example | HashTable Class Declaration, Parameters, Methods, Constructors with Examples

Want to learn about the Java Hashtable? Then, this is the best & ultimate tutorial for you all. Hashtable stores the key/values pair in a hash table. Both HashMap and HashTable are similar but there are few differences between them which can be known clearly from here.

Also, we will see how to create a Hashtable, how it works internally, methods of java hashtable, some example codes, etc. So, stay tuned to this page and gather enough information on Hashtable Class in Java.

Hashtable in Java

Java Hashtable Class inherits Dictionary class and implements the Map interface. Also, This class implements a hash table. Moreover, it was a part of java.util package. Java Hashtable is the same as HashMap besides it is synchronized.

Java Hashtable class declaration

Here is the declaration for java.util.Hashtable class:

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

Parameters of HashTable Class

  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.

The Hierarchy of Hashtable Class in Java

hierarchy of hashtable class in java

Do Read:

How Hashtable works internally in java?

A Hashtable internally holds buckets in which it stores the key/value pairs. The Hashtable uses the key’s hashcode to specify which bucket the key/value pair should map. When you give a key/value to the Hashtable, it asks the key’s hashcode. The Hashtable uses that code to determine the bucket in which to place the key/value.

Thus, for instance, if the hashcode equals zero, the Hashtable stores the value into Bucket 0. Furthermore, if the hashcode is two, the Hashtable stores the value into Bucket 2. (This is a simple example; the Hashtable will massage the hashcode first so the Hashtable doesn’t try to insert the value outside the bucket.)

With the help of this hashcode approach, the Hashtable can also instantly determine in which bucket it has stored the value when you decide to retrieve it. The hashcode only says the Hashtable into which bucket to drop the key/value. Sometimes, still, multiple objects may map to the same bucket, an event called a collision. In Java, the Hashtable reacts to a collision by storing multiple values into the same bucket (other implementations may work collisions differently).

Features about Java Hashtable

  1. Java Hashtable is the best option in case of frequent operation is to store and retrieves objects from a hash table.
  2. Java Hashtable uses a hash table data structure.
  3. It implements a Serializable, Cloneable interface but not RandomAccessInterface.
  4. Hashtable is thread-safe because every method determines in the Java Hashtable class is synchronized.
  5. Insertion order is not stored and it depends on the hash code of keys.
  6. Duplicates keys are not accepted but values may be duplicated.
  7. Heterogeneous objects are allowed.
  8. Java Hashtable class includes unique elements.
  9. Java Hashtable doesn’t support null keys or null values.

Constructors of Java Hashtable Class

Java Hashtable represents the following constructors.

  1. Hashtable(): The first constructor is used to create a default hash table.
  2. Hashtable(int size): This constructor is used to creates a hash table that has an initial size specified by size.
  3. Hashtable(int size, float load factor): This constructor is used to creates a hash table that has an initial size specified by size and a load factor is specified by the load factor.
  4. Hashtable(Map m): This constructor is used to creates a hash table that is initialized with the elements in m.

Methods of Hashtable Class in Java

  1. void clear(): This method is used to removes all key/values pair from a hash table and make it empty.
  2. Object clone(): It creates a shallow copy of this hash table.
  3. boolean contains(Object value): This method returns true if some value equal to value exists within the hash table and returns false if the key is not found.
  4. boolean containsKey(Object key): This method returns true if some key equal to key exists within the hash table else it returns false.
  5. boolean containsValue(Object value): It returns true if some value equal to value exists within the hash table else it returns false.
  6. Enumeration elements(): It returns an enumeration of the values contained in the hash table.
  7. Object get(Object key): This method is used to remove the object that contains the value associated with a key and returned a null object if the key is not in the hash table.
  8. boolean isEmpty(): It returns true if the hash table is empty else returns false.
  9. Enumeration keys(): It returns an enumeration of the keys contained in the hash table.
  10. Object put(Object key, Object value): This method is used to insert a key-value pair in the hash table.
  11. void rehash(): This method is used to increases the size of the hash table and rehashes all of its keys.
  12. Object remove(Object key): It removes a key and its value and returned a null object if the key is not in the hash table.
  13. int size(): It returns the number of entries in the hash table.
  14. String toString(): This method is used to returns the string equivalent of a hash table.

Java Hashtable Example

import java.util.*;
class HashtableExample{
public static void main(String args[]){

//creating a hash table
Hashtable<String, String> ht = new Hashtable<String, String>();

Enumeration names;
String key;

//adding values in this hash table
ht.put("Key 1", "Rahul");
ht.put("Key 2", "Amit");
ht.put("Key 3", "Vijay");
ht.put("Key 4", "Suresh");
ht.put("Key 5", "Prashant");

//returns size of the hash table
System.out.println("Hash table size is: "+ht.size());

//checks hash table is empty or not
System.out.println("Hash table is empty or not: "+ht.isEmpty());

//returns true if hash table contains specified value
System.out.println("Hash table contains specified value or not: "+ht.containsValue("Prashant"));

names = ht.keys();
while(names.hasMoreElements()){
key = (String)names.nextElement();
System.out.println("Key is: " +key + " & its Value is: " +ht.get(key));
}

ht.clear();
System.out.println("Removes all the key-value pair: "+ht);
}
}

Output:

Java Hashtable with Example 1

Hashtable vs HashMap

Hashtable and HashMap both are part of the java.util and are utilized to save data in the form of key-value pairs. Hashing techniques are used by both to store unique keys. However, there are few differences between Hashtable and HashMap which is very essential to know for achieving the Java related interviews. Let’s focus on the differences between Hashtable and HashMap by viewing the following points:

  1. Hashtable is synchronized while the HashMap is not synchronized.
  2. Hashtable doesn’t allow you to store null key and null value on the other hand HashMap allows you to store one null key and multiple null values.
  3. HashMap was introduced in Java 1.2 while Hashtable is a legacy class.
  4. HashMap is fast while Hashtable is slow due to synchronization.
  5. HashMap is traversed by Iterator while Hashtable is traversed by both Enumeration and Iterator.
  6. HashMap inherits the AbstractMap class on the other hand Hashtable inherits the dictionary class.

Comparison Table of HashMap & HashTable

Java Hashtable with Example 2