Hashtable in Java

Naveen AnamaneniUpdated · 3 min read

Hashtable in Java is a class that stores key-value pairs in a hash table, much like HashMap. It is used to look up values by key. Unlike HashMap, every method of Hashtable is synchronized, and it allows no null keys and no null values. Hashtable has existed since Java 1.0 and is a legacy class; in Java 1.2 it was updated to implement Map. In this lesson, you will learn how Hashtable works and what modern code uses instead.

Declaration

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

Dictionary is an even older abstract class, now obsolete.

Features of Hashtable

  • Key-value pairs with unique keys.
  • Synchronized: only one thread can use it at a time.
  • No null: a null key or value throws NullPointerException.
  • No guaranteed order.
  • Default capacity 11, load factor 0.75; it grows to 2 x old + 1.
  • Legacy methods: elements() and keys() return an Enumeration.

Example: Using Hashtable

Let's see an example of the main operations and the null rule.

HashtableDemo.java▶ Try it
import java.util.Enumeration;
import java.util.Hashtable;
 
public class HashtableDemo {
    public static void main(String[] args) {
        Hashtable<String, Integer> marks = new Hashtable<>();
        marks.put("Navika", 91);
        marks.put("Srihitha", 78);
        marks.put("Advitha", 85);
 
        System.out.println("Navika: " + marks.get("Navika"));
        System.out.println("Size: " + marks.size());
 
        Enumeration<String> keys = marks.keys();
        int count = 0;
        while (keys.hasMoreElements()) {
            keys.nextElement();
            count++;
        }
        System.out.println("Keys seen: " + count);
 
        try {
            marks.put("Anamaneni", null);
        } catch (NullPointerException e) {
            System.out.println("null value rejected");
        }
    }
}
Output
Navika: 91
Size: 3
Keys seen: 3
null value rejected

Explanation: put and get work exactly as in a HashMap. keys() returns an old-style Enumeration. Trying to store a null value throws NullPointerException, which a HashMap would have accepted.

Why Hashtable Is Not Used Today

  • Slow: every call takes a lock on the whole table, even in single-threaded code.
  • Poor concurrency: with many threads, only one can read or write at a time.
  • Better options exist: HashMap for one thread, and ConcurrentHashMap for many threads, which lets many threads read and write at once.

Common Mistakes

Storing null values. Hashtable throws; HashMap allows them.

Choosing Hashtable for thread safety in new code. ConcurrentHashMap is faster and safer for compound updates through merge and compute.

Practice

  1. Easy: Rewrite the example with HashMap and a for-each loop over entrySet().
  2. Harder: Write a method that copies a Hashtable into a TreeMap and prints it in sorted key order.

Quick check: Hashtable

4 questions
1Does Hashtable allow null keys?
2Are Hashtable's methods synchronized?
3What should multi-threaded code use instead of Hashtable?
4Which class does Hashtable extend?

Key Takeaways

Was this tutorial helpful?

Lesson 289 of 482