What is HashTable in Java ?

HashTable class in Java implements a hash table, It  maps keys to values. Similar to HashMap, it also stores the data in key,value pair.  The key is hashed using hashing functionality and the resulting hash code is used as the index at which the value is stored within the table.
To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

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

Important points to remember about HashTable in Java :

  • HashTable stores the data in key/value pair.
  • HashTable is a thread-safe class as it implements synchronization technique in its implementation.
  • Unlike HashMap, In HashTable null key / value is not allowed.
  • In Hashtable , hashing functionality is computed on key to find the address to store the value.

Constructors present in HashTable class:

Sr.No Constructor & Description
1 Hashtable()

This is the default constructor and used for instantiating HashTable class.

2 Hashtable(int size)

It is parameterized constructor and it creates a instance of HashTable class that has initial size specified by size.

3 Hashtable(int size, float fillRatio)

This constructor creates a hash table that has initial size specified by size and fill ratio specified by fillRatio.

fill ratio basically determines how full hash table can be before it is resized upward and its Value lie between 0.0 to 1.0

4 Hashtable(Map m)

This creates a hash table that is initialised with the elements in m

 

Methods present in HashTable class:

Sr.No Method & Description
1 void clear( )

This method is used to clear / empty the HashTable.

2 Object clone( )

This method returns the exact copy/replica of the invoking object.

3 boolean contains(Object value)

This method checks if the value present in parameter exists in the HashTable or not. In case the value is present , it  returns true otherwise false.

4 boolean containsKey(Object key)

This method checks if the value present in parameter exists in the HashTable “key”or not. In case the key is present , it  returns true otherwise false.

5 boolean containsValue(Object value)

This method checks if the value present in parameter exists in the HashTable “value” or not. In case the value is present , it  returns true otherwise false.

6 Enumeration elements( )

This method returns an enumeration of the values present in the hash table.

7 Object get(Object key)

This method returns the object that contains the value associated with the key. If the key is not in the hash table, a null object is returned.

8 boolean isEmpty( )

This method checks if the hash table is empty and returns true if it is empty otherwise it returns false.

9 Enumeration keys( )

This method returns an enumeration of the keys present in the hash table.

10 Object put(Object key, Object value)

This method is used to insert a key and a value into the hash table. It returns null if the key isn’t already in the hash table; returns the previous value associated with the key if the key is already in the hash table.

11 void rehash( )

It is used to increases the size of the hash table and rehashes all of its keys.

12 Object remove(Object key)

It removes the key and its value. It returns the value associated with the key. If the key is not in the hash table, a null object is returned.

13 int size( )

This method returns the number of entries present in the hash table.

14 String toString( )

This method returns a String equivalent to the hashtable.

Example 1 : Illustrative example of the HashTable in Java

import java.util.*;

 

class hashTableExample1 {

public static void main(String[] arg)
{

// code to create a hash table
Hashtable<Integer, String> ht =
new Hashtable<Integer, String>();

 

Hashtable<Integer, String> ht2 =
new Hashtable<Integer, String>();

 

ht.put(1, “Tech”);
ht.put(2, “Blog”);
ht.put(3, “Station”);

 

// code to create a clone or shallow copy of existing hashtable
ht2 = (Hashtable<Integer, String>)ht.clone();

 

System.out.println(“values present in clone: ” + ht2);

 

// code to clear hashtable
ht.clear();

 

System.out.println(“after clearing: ” + ht2);
}
}

 

 

Output :

values present in clone: {1=Tech, 2=Blog, 3=Station}
after clearing: {}

Thanks for reading this article. I hope you find it useful. Please post your comments / feedback / questions / suggestions in the below comment box.

You can also connect directly with me using social media accounts.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply