WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Intermediate · question 21 of 100

What is the difference between HashMap and HashTable in Java Collections?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

HashMap and HashTable are two implementations of the Map interface in the Java Collections Framework. While they both serve the same purpose of storing key-value pairs, there are some key differences between the two:

Thread-safety: HashTable is thread-safe, which means it can be safely used in concurrent environments without the need for external synchronization. HashMap is not thread-safe by default, but it can be made thread-safe by wrapping it using the Collections.synchronizedMap() method.

Null values: HashTable does not allow null keys or values, while HashMap allows one null key and any number of null values.

Iteration order: HashTable does not guarantee any particular order of iteration over its elements, while HashMap does not guarantee any specific order of iteration by default, but it does provide a LinkedHashmap implementation that preserves insertion order.

Performance: HashMap is generally considered to have better performance than HashTable, as HashTable uses synchronization for thread-safety, which can add overhead.

Here’s an example of how to use HashMap and HashTable in Java:

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class Example {
    public static void main(String[] args) {
        // create a HashMap of strings
        Map<String, String> myHashMap = new HashMap<>();
        myHashMap.put("key1", "value1");
        myHashMap.put("key2", "value2");
        myHashMap.put(null, "value3");
        
        // create a HashTable of strings
        Hashtable<String, String> myHashTable = new Hashtable<>();
        myHashTable.put("key1", "value1");
        myHashTable.put("key2", "value2");
        
        // iterate over the elements of the HashMap
        for (Map.Entry<String, String> entry : myHashMap.entrySet()) {
          System.out.println(entry.getKey() + ": " + entry.getValue());
        }
        
        // iterate over the elements of the HashTable
        for (Map.Entry<String, String> entry : myHashTable.entrySet()) {
          System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

In this example, we create a HashMap of strings called "myHashMap" and add three key-value pairs to it, including one with a null key. We also create a HashTable of strings called "myHashTable" and add two key-value pairs to it. We then iterate over the elements of both collections using the entrySet() method and use the println() method to print out each key-value pair.

The output of this program would be:

null: value3
key1: value1
key2: value2
key2: value2
key1: value1

As you can see, HashMap allows null keys and values, while HashTable does not. Also, the order of iteration over the elements of HashMap is not guaranteed, while the order of iteration over the elements of HashTable is in the order they were inserted.

In summary, while HashMap and HashTable are both implementations of the Map interface and serve the same purpose of storing key-value pairs, they have some key differences in terms of thread-safety, handling of null values, iteration order, and performance. Which one to use depends on the specific use case and requirements of your program.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics