Differences Between HashMap and Hashtable in Java

Java Q&A Hub 使用 GPT-4o-mini 回答

问题:

What are the differences between HashMap and HashTable in Java? Interpretation from the perspective of Java source code

回答:

In Java, both HashMap and Hashtable are part of the Java Collections Framework and are used to store key-value pairs. However, there are several key differences between them, which can be understood from both a conceptual and source code perspective. Here are the main differences:

1. Synchronization

  • HashMap: It is not synchronized, which means it is not thread-safe. Multiple threads can access a HashMap concurrently without any external synchronization, which can lead to inconsistent data if one thread modifies the map while another is reading it.

  • Hashtable: It is synchronized, meaning it is thread-safe. All methods in Hashtable are synchronized, which prevents concurrent access issues. However, this can lead to performance overhead due to the locking mechanism.

2. Null Keys and Values

  • HashMap: It allows one null key and multiple null values. This means you can store a key-value pair where the key is null or the value is null.

  • Hashtable: It does not allow null keys or null values. Attempting to insert a null key or value will throw a NullPointerException.

3. Iteration

  • HashMap: It uses an iterator for traversing the keys or values, which is fail-fast. If the map is modified during iteration (except through the iterator's own remove method), it will throw a ConcurrentModificationException.

  • Hashtable: It uses an enumerator for traversing the keys or values, which is not fail-fast. Modifications to the Hashtable during iteration do not throw an exception, but the results of the iteration may be unpredictable.

4. Performance

  • HashMap: Generally, HashMap is faster than Hashtable because it is not synchronized. The lack of synchronization allows for better performance in single-threaded or externally synchronized scenarios.

  • Hashtable: The synchronization overhead makes Hashtable slower compared to HashMap, especially in scenarios where high concurrency is not required.

5. Legacy vs. Modern

  • HashMap: It is part of the Java Collections Framework introduced in Java 2 (Java 1.2) and is considered a modern collection class.

  • Hashtable: It is a legacy class that has been part of Java since version 1.0. While it is still available, it is generally recommended to use HashMap or other modern collections.

6. Implementation

From a source code perspective, you can observe the following:

  • HashMap: The implementation of HashMap uses a combination of an array and linked lists (or trees in the case of high collision) to store entries. It has methods like put(), get(), and remove() that are not synchronized.

  • Hashtable: The implementation of Hashtable is similar to HashMap, but all of its methods are synchronized. This can be seen in the source code where methods like put(), get(), and remove() are wrapped in synchronized blocks.

Example Code Snippet

Here’s a simplified view of how the two classes might look in terms of synchronization:

// HashMap (simplified)
public class HashMap<K, V> {
    // Internal data structure
    private Entry<K, V>[] table;

    public V put(K key, V value) {
        // Implementation without synchronization
    }

    public V get(Object key) {
        // Implementation without synchronization
    }
}

// Hashtable (simplified)
public class Hashtable<K, V> {
    // Internal data structure
    private Entry<K, V>[] table;

    public synchronized V put(K key, V value) {
        // Synchronized implementation
    }

    public synchronized V get(Object key) {
        // Synchronized implementation
    }
}

Conclusion

In summary, HashMap is generally preferred for non-threaded applications due to its performance advantages and flexibility with null values. Hashtable is considered a legacy class and is rarely used in modern Java applications, with ConcurrentHashMap being a better alternative for thread-safe operations.