The perfect place for easy learning...

Java Programming

×

Topics List


Hashtable class in java





In java, the package java.util contains a class called Hashtable which works like a HashMap but it is synchronized. The Hashtable is a concrete class of Dictionary. It is used to store and manage elements in the form of a pair of key and value.

The Hashtable stores data as a pair of key and value. In the Hashtable, each key associates with a value. Any non-null object can be used as a key or as a value. We can use the key to retrieve the value back when needed.

🔔 The Hashtable class is no longer in use, it is obsolete. The alternate class is HashMap.

🔔 The Hashtable class is a concrete class of Dictionary.

🔔 The Hashtable class is synchronized.

🔔 The Hashtable does no allow null key or value.

🔔 The Hashtable has the initial default capacity 11.

The Hashtable class in java has the following constructors.

S. No. Constructor with Description
1 Hashtable( )

It creates an empty hashtable with the default initial capacity 11.

2 Hashtable(int capacity)

It creates an empty hashtable with the specified initial capacity.

3 Hashtable(int capacity, float loadFactor)

It creates an empty hashtable with the specified initial capacity and loading factor.

4 Hashtable(Map m)

It creates a hashtable containing elements of Map m.

The Hashtable class in java has the following methods.

S. No. Methods with Description
1 V put(K key, V value)

It inserts the specified key and value into the hash table.

2 void putAll(Map m))

It inserts all the elements of Map m into the invoking Hashtable.

3 V putIfAbsent(K key, V value)

If the specified key is not already associated with a value associates it with the given value and returns null, else returns the current value.

4 V getOrDefault(Object key, V defaultValue)

It returns the value associated with given key; or defaultValue if the hashtable contains no mapping for the key.

5 V get(Object key)

It returns the value associated with the given key.

6 Enumeration keys()

Returns an enumeration of the keys of the hashtable.

7 Set keySet()

Returns a set view of the keys of the hashtable.

8 Collection values()

It returns a collection view of the values contained in the Hashtable.

9 Enumeration elements()

Returns an enumeration of the values of the hashtable.

10 Set entrySet()

It returns a set view of the mappings contained in the hashtable.

11 int hashCode()

It returns the hash code of the hashtable.

12 Object clone()

It returns a shallow copy of the Hashtable.

13 V remove(Object key)

It returns the value associated with given key and removes the same.

14 boolean remove(Object key, Object value)

It removes the specified values with the associated specified keys from the hashtable.

15 boolean contains(Object value)

It returns true if the specified value found within the hash table, else return false.

16 boolean containsValue(Object value)

It returns true if the specified value found within the hash table, else return false.

17 boolean containsKey(Object key)

It returns true if the specified key found within the hash table, else return false.

18 V replace(K key, V value)

It replaces the specified value for a specified key.

19 boolean replace(K key, V oldValue, V newValue)

It replaces the old value with the new value for a specified key.

20 void replaceAll(BiFunction function)

It replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

21 void rehash()

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

22 String toString()

It returns a string representation of the Hashtable object.

23 V merge(K key, V value, BiFunction remappingFunction)

If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.

24 void forEach(BiConsumer action)

It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.

25 boolean isEmpty( )

It returns true if Hashtable has no elements; otherwise returns false.

26 int size( )

It returns the total number of elements in the Hashtable.

27 void clear()

It is used to remove all the lements of a Hashtable.

28 boolean equals(Object o)

It is used to compare the specified Object with the Hashtable.

Let's consider an example program to illustrate methods of Hashtable class.

Example
import java.util.*;

public class HashtableExample {

	public static void main(String[] args) {

		Random num = new Random(); 
		Hashtable table = new Hashtable();
		
		//put(key, value)
		for(int i = 1; i <= 5; i++)
			table.put(i, num.nextInt(100));
		
		System.out.println("Hashtable => " + table);
		
		//get(key)
		System.out.println("\nValue associated with key 3 => " + table.get(3));
		System.out.println("Value associated with key 30 => " + table.get(30));
		
		//keySet()
		System.out.println("\nKeys => " + table.keySet());

		//values()
		System.out.println("\nValues => " + table.values());
		
		//entrySet()
		System.out.println("\nKey, Value pairs as a set => " + table.entrySet());

		//hashCode()
		System.out.println("\nHash code => " + table.hashCode());

		//hashCode()
		System.out.println("\nTotal number of elements => " + table.size());

		//isEmpty()
		System.out.println("\nEmpty status of Hashtable => " + table.isEmpty());
	}
}

When we execute the above code, it produce the following output.

Hashtable class in java