Câu hỏi phỏng vấn Java
Câu hỏi

What differences exist between HashMap and Hashtable ?

Câu trả lời

When answering the interview question about the differences between HashMap and Hashtable in Java, you can highlight several key points:

  1. Synchronization and Thread Safety:

    • HashMap is non-synchronized, meaning it is not thread-safe and cannot be shared between multiple threads without proper synchronization code[1][2][3][4][6][7].
    • Hashtable is synchronized, making it thread-safe and suitable for use in multi-threaded environments where it can be shared between threads[1][2][3][4][6][7].
  2. Null Keys and Values:

    • HashMap allows one null key and multiple null values, which can be useful in certain scenarios where you need to represent the absence of a value[3][5][6][7].
    • Hashtable does not allow any null key or value, attempting to insert nulls will result in a NullPointerException[3][5][6][7].
  3. Performance:

    • Due to the lack of synchronization, HashMap is generally faster and more efficient in scenarios where thread safety is not a concern[3][5][6].
    • Hashtable has a performance cost due to its synchronization feature, which can slow down operations when used in single-threaded applications[5][6].
  4. Inheritance and Framework Membership:

    • HashMap is a subclass of AbstractMap and has been a part of the Java Collections Framework since its introduction in JDK 1.2[2][3][5].
    • Hashtable is a legacy class that was originally part of JDK 1.0 and later re-engineered to implement the Map interface to become a member of the collection framework[2][3][5].
  5. Iterator Behavior:

    • The iterators returned by HashMap are fail-fast, meaning they will throw a ConcurrentModificationException if the map is modified while ...
junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What is a Server Side Include (SSI)?

middle

How and where are Annotations used in Java?

middle

When is the finalize() called? What is the purpose of finalization?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào