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

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup