Java Hashtable Class

Introduction

The Hashtable class in Java is a part of the java.util package.

It is a legacy class that implements a hash table, which maps keys to values. 

It is synchronized and can be used in multithreaded environments.

Table of Contents

  1. What is the Hashtable Class?
  2. Common Methods
  3. Examples of Using the Hashtable Class
  4. Conclusion

1. What is the Hashtable Class?

The Hashtable class implements a hash table data structure, which allows you to store and retrieve key-value pairs. It is synchronized, meaning it is thread-safe and can be shared among multiple threads without additional synchronization.

2. Common Methods

  • put(K key, V value): Maps the specified key to the specified value.
  • get(Object key): Returns the value to which the specified key is mapped.
  • remove(Object key): Removes the key (and its corresponding value) from the hashtable.
  • containsKey(Object key): Returns true if the hashtable contains the specified key.
  • containsValue(Object value): Returns true if the hashtable maps one or more keys to the specified value.
  • size(): Returns the number of key-value mappings in the hashtable.
  • isEmpty(): Returns true if the hashtable contains no key-value mappings.
  • keys(): Returns an enumeration of the keys in the hashtable.
  • elements(): Returns an enumeration of the values in the hashtable.
  • clear(): Clears the hashtable so that it contains no key-value mappings.

3. Examples of Using the Hashtable Class

Example 1: Creating and Using a Hashtable

This example demonstrates how to create and use a Hashtable with key-value pairs.

import java.util.Hashtable;

public class HashtableExample {
    public static void main(String[] args) {
        Hashtable<String, Integer> hashtable = new Hashtable<>();

        // Adding elements
        hashtable.put("Apple", 1);
        hashtable.put("Banana", 2);
        hashtable.put("Cherry", 3);

        // Retrieving elements
        System.out.println("Value for 'Apple': " + hashtable.get("Apple"));

        // Checking if key exists
        System.out.println("Contains key 'Banana': " + hashtable.containsKey("Banana"));

        // Checking if value exists
        System.out.println("Contains value 3: " + hashtable.containsValue(3));

        // Removing an element
        hashtable.remove("Banana");
        System.out.println("After removing 'Banana': " + hashtable);

        // Getting the size of the hashtable
        System.out.println("Size of hashtable: " + hashtable.size());
    }
}

Output:

Value for 'Apple': 1
Contains key 'Banana': true
Contains value 3: true
After removing 'Banana': {Cherry=3, Apple=1}
Size of hashtable: 2

Example 2: Iterating Over Keys and Values

This example shows how to iterate over the keys and values in a Hashtable.

import java.util.Enumeration;
import java.util.Hashtable;

public class HashtableIterationExample {
    public static void main(String[] args) {
        Hashtable<String, Integer> hashtable = new Hashtable<>();
        hashtable.put("Apple", 1);
        hashtable.put("Banana", 2);
        hashtable.put("Cherry", 3);

        // Iterating over keys
        Enumeration<String> keys = hashtable.keys();
        System.out.println("Keys:");
        while (keys.hasMoreElements()) {
            System.out.println(keys.nextElement());
        }

        // Iterating over values
        Enumeration<Integer> values = hashtable.elements();
        System.out.println("Values:");
        while (values.hasMoreElements()) {
            System.out.println(values.nextElement());
        }
    }
}

Output:

Keys:
Cherry
Apple
Banana
Values:
3
1
2

Example 3: Clearing the Hashtable

This example demonstrates how to clear all key-value mappings from a Hashtable.

import java.util.Hashtable;

public class HashtableClearExample {
    public static void main(String[] args) {
        Hashtable<String, Integer> hashtable = new Hashtable<>();
        hashtable.put("Apple", 1);
        hashtable.put("Banana", 2);
        hashtable.put("Cherry", 3);

        System.out.println("Hashtable before clear: " + hashtable);

        // Clearing the hashtable
        hashtable.clear();

        System.out.println("Hashtable after clear: " + hashtable);
        System.out.println("Is hashtable empty? " + hashtable.isEmpty());
    }
}

Output:

Hashtable before clear: {Cherry=3, Apple=1, Banana=2}
Hashtable after clear: {}
Is hashtable empty? true

4. Conclusion

The Hashtable class in Java provides a synchronized key-value mapping, making it suitable for use in multithreaded environments. Although it has largely been replaced by the HashMap class due to its better performance in non-synchronized contexts, Hashtable is still useful when thread safety is required without additional synchronization. The examples provided demonstrate common usage patterns and highlight the capabilities of the Hashtable class.

Comments