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

What are the differences between a **H...

Câu trả lời

Certainly, when discussing the differences between HashMap and Hashtable in Java, there are several key points to consider:

  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][5][6].
    • Hashtable, on the other hand, is synchronized. It is thread-safe and can be shared between multiple threads, as each method in Hashtable is synchronized[1][2][3][4][5][6].
  2. Null Keys and Values:

    • HashMap allows one null key and multiple null values, which provides more flexibility in storing null entries[2][4][5][6].
    • Hashtable does not allow any null key or null value. Attempting to insert nulls into a Hashtable will result in a NullPointerException[2][4][5][6].
  3. Performance:

    • Due to its non-synchronized nature, HashMap is generally faster and has higher performance in scenarios where thread safety is not a concern[4][5][6].
    • Hashtable's synchronized methods mean that it has a performance cost, which can make it slower compared to HashMap when used in a single-threaded context[4][5][6].
  4. Legacy Status:

    • Hashtable is considered a legacy class as it was introduced in JDK 1.0. From JDK 1.2, it was re-engineered to implement the Map interface to become a member of the Java Collections Framework[2][5].
    • HashMap is a newer class, introduced in JDK 1.2, and was designed to be a part of the Java Collections Framework from the beginning[2][5].
  5. Iterators:

    • The iterators returned by HashMap are fail-fast, meaning they will throw a ConcurrentModificationException if the map is modified while iterating over it[2][4].
    • Hashtable provides enumerations and iterators, which are not fail-fast[2].
  6. Inheritance and Implementation:

    • HashMap extends the AbstractMap c...
senior

senior

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

senior

What is the purpose of using RMISecurityManager in RMI?

middle

Explain the role of Driver in JDBC.

middle

Can you access non static variable in static context?

Bình luận

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

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