Java – Hashtables

Hashtables in Java

In this lesson, I would teach you all you need to be able to use Hashtables in Java.

A hashtable in Java is an implementation of the dictionary data structure. Therefore you can use it for storage of key-values pairs. This is similar to hashmaps. But unlike hashmaps, hashtables are thread-safe (or synchronized).

 

We cover the following:

  1. How Hashtables Work
  2. Hashtable Methods
  3. Hashtable Example

 

1. How Hashtables Work

To store an item in a hashtable, you need to specify the value you want to store. Also you must specify a key along with this value. Then this  key is then hashed. This means passing it through a hash function. The output of the hashing is a hash code which represents the index where the value is to be stored.

 

How to Create a Hashtable

You can create a hashtable using the new keyword. An example is given below:

	Hashtable ht = new Hashtable();
	Hashtable ht2 = new Hashtable(10);

The first line creates a new hashtable while the second line creates a new hashtable with a size of 10. You can also use any of the constructors below.

 

SN Hashtable constructors
1 Hashtable( )

The default constructor of the hash table. It creates a new hashtable.

2 Hashtable(int size)

Takes the initial size of the table as parameter and then creates a hash table that with an initial size given by the value size.

3 Hashtable(int size, float fillRatio)

This creates a hash table with an initial size given by size parameter and also a fill ratio. The fill ratio must be between 0.0 and 1.0. It then determines how full the hash table can be before it is resized.

4 Hashtable(Map < ? extends K, ? extends V > t)

This constructor creates a Hashtable with the given mappings.

Table 1.1. Hashtable Constructors

 

 

2. Hashtable Methods

You can use the following methods the perform various operations on a hashtable. Similar to arraylist, you can add and delete items from a hashtable.

Sr.No Method & Description
1 clear( )

This is used to clear (or empty) the hash table.

2 clone( )

Used to return a duplicate of the invoking hashtable. Create another copy

3 contains(Object value)

You use this to check if the hashtable contains the specified value. It true if  the object exists within the hash table. Otherwise, it returns false

4 containsKey(Object key)

You use this to check if the hashtable contains the specified key. It true if  the key exists within the hash table. Otherwise, it returns false

5 containsValue(Object value)

You use this to check if the hashtable contains the specified value. It true if  the value exists within the hashtable. Otherwise, it returns false

6 elements( )

Used to return an enumeration of all the values contained in the hashtable.

7 get(Object key)

Used to return 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 isEmpty( )

Used to check if the hashtable is empty. Return true if the hash table is empty. Otherwise returns false.

9 keys( )

List of the keys contained in the hash table.

10 put(Object key, Object value)

You use this to add a key and a value pair into the hash table. It returns null if the key is not already existing in the hashtable; returns the previous value associated with the key if the key is already in the hashtable.

11 rehash( )

It Increases the size of the hashtable and then rehashes all of its keys.

12 remove(Object key)

Removes the specified key along with its value. Returns the value associated with the specified key. If the key is not found in the hashtable, a null object is returned.

13 size( )

Returns an integer value of the number of entries in the hashtable.

14 String toString( )

Creates the string equivalent of a hashtable.

Table 1.1: Hashtable Methods

 

3. Hashtable Example

In the example below, we create a hashtable and insert some items. Then remove an item. We also demonstrate the use of some other methods of the hash table. I therefor suggest you try this code on your own.

 

import java.util.Hashtable;

public class HashTableDemo {

	public static void main(String[] args) {
		
	Hashtable ht = new Hashtable();
	ht.put(1, "Monday");
	ht.put(2, "Tuesday");	
	ht.put(3, "Wednesday");	
	ht.put(4, "Thursday");	
	ht.put(5, "Friday");	
	ht.put(6, "Saturday");	
	ht.put(7, "Sunday");	
	
	System.out.println("Is Empty? " + ht.isEmpty());
	System.out.println("Contains Tuesday? " + ht.contains("Tuesday"));
	System.out.println("Value of 5: " + ht.get(5));
	System.out.println("Number of entries: " + ht.size());
	
	System.out.println(ht.toString());
	}
}

Listing 1.1: Demo of hashtables

 

If you run the code, you would have the output below:

Is Empty? false
Contains Tuesday? true
Value of 5: Friday
Number of entries: 7
{7=Sunday, 6=Saturday, 5=Friday, 4=Thursday, 3=Wednesday, 2=Tuesday, 1=Monday}