問題描述
我只是在閱讀 Java 中 HashMap 和 HashTable 類之間的區別.在那里我發現了一個區別,前者允許空鍵,而后者不允許相同的權限.就 HashMap 的工作而言,我知道,它在 key 上調用 hashcode 方法來查找要放置該鍵值對的存儲桶.我的問題來了:如何計算空值的哈希碼或空鍵的哈希碼是否有任何默認值(如果有,請指定值)?
I was just reading about the difference between HashMap and HashTable class in java. There I found a difference that former allow null key and later doesn't privileges for the same. As far as the working of HashMap is concern I know that, it calls hashcode method on key for finding the bucket in which that key value pair is to be placed. Here comes my question: How hashcode for a null value is computed or Is there any default value for hashcode of null key (if so please specify the value)?
推薦答案
from HashMap:
from HashMap:
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
...
如果你進一步看,你會發現 null 總是進入 bin 0
and if you look further you will see that null always goes to bin 0
這篇關于HashMap 中 NULL 鍵的哈希碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!