Java - Reasons for Allowing null as a HashMap Key?

后端 未结 1 901
你的背包
你的背包 2021-02-10 03:13

Why Java decided to allow \'null\' as a key in HashMap? As we know hashcode() can\'t be calculated on null value. Is there any design consideration for this decision to allow nu

1条回答
  •  时光说笑
    2021-02-10 03:52

    From the JDK 1.2 Java Collections API Change Summary (not sure where to find the official version on Oracle's website):

    Added null-key support to HashMap. This was done for consistency with TreeMap and the late, unlamented ArrayMap, and because customers requested it. Now all of our general-purpose collection implementations accept null keys, values and elements.

    Joshua Bloch and Doug Lea disagreed on this, and this caused problems for concurrent hash maps.

    0 讨论(0)
提交回复
热议问题