Explain HashMap vs Hashtable in Java

๐Ÿ’ก Concept: HashMap vs Hashtable

HashMap and Hashtable are implementations of the Map interface in Java, with differences in synchronization and null handling.

๐Ÿ“˜ Quick Intro

HashMap is unsynchronized and allows null keys and values; Hashtable is synchronized and does not allow null keys or values.

๐Ÿง  Analogy

Think of Hashtable as a thread-safe safe with restricted inputs, while HashMap is a more flexible but unsafe locker.

๐Ÿ”ง Technical Explanation

  • HashMap is not synchronized, so not thread-safe.
  • Hashtable is synchronized and thread-safe.
  • HashMap allows one null key and multiple null values.
  • Hashtable does not allow any null key or value.
  • HashMap is generally faster due to lack of synchronization overhead.

๐ŸŽฏ Use Cases

  • โœ… Use HashMap in single-threaded or externally synchronized environments.
  • โœ… Use Hashtable in legacy thread-safe code.
  • โœ… Prefer ConcurrentHashMap for modern thread-safe requirements.

๐Ÿ’ป Code Example: HashMap vs Hashtable


import java.util.*;

public class MapExample {
    public static void main(String[] args) {
        Map hashMap = new HashMap<>();
        Map hashTable = new Hashtable<>();

        hashMap.put(null, "Value1"); // Allowed
        // hashTable.put(null, "Value2"); // Throws NullPointerException

        System.out.println("HashMap: " + hashMap);
        System.out.println("Hashtable: " + hashTable);
    }
}

โ“ Interview Q&A

Q1: Is HashMap synchronized?
A: No.

Q2: Does Hashtable allow null keys?
A: No.

Q3: Which is faster?
A: HashMap.

Q4: Is Hashtable thread-safe?
A: Yes.

Q5: Can HashMap contain null values?
A: Yes.

Q6: Which one is legacy?
A: Hashtable.

Q7: How to make HashMap synchronized?
A: Using Collections.synchronizedMap().

Q8: What exceptions if null key in Hashtable?
A: NullPointerException.

Q9: Can HashMap be used in multi-threaded environment?
A: Not without synchronization.

Q10: Which class implements Map interface?
A: Both.

๐Ÿ“ MCQs

Q1. Is HashMap synchronized?

  • Yes
  • No
  • Sometimes
  • Always

Q2. Does Hashtable allow null keys?

  • Yes
  • No
  • Sometimes
  • Always

Q3. Which is faster?

  • Hashtable
  • HashMap
  • Both same
  • None

Q4. Is Hashtable thread-safe?

  • No
  • Yes
  • Sometimes
  • Never

Q5. Can HashMap contain null values?

  • No
  • Yes
  • Sometimes
  • Never

Q6. Which one is legacy?

  • HashMap
  • Hashtable
  • ConcurrentHashMap
  • None

Q7. How to make HashMap synchronized?

  • Using synchronized keyword
  • Using Collections.synchronizedMap()
  • Using ReentrantLock
  • No way

Q8. What exceptions if null key in Hashtable?

  • ClassCastException
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • IllegalArgumentException

Q9. Can HashMap be used in multi-threaded environment?

  • Yes
  • No
  • Not without synchronization
  • Sometimes

Q10. Which class implements Map interface?

  • HashMap only
  • Hashtable only
  • Both
  • None

๐Ÿ’ก Bonus Insight

Choosing between HashMap and Hashtable depends on synchronization needs and legacy code considerations.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: