問題描述
我只是在閱讀 Java 中 HashMap 和 HashTable 類之間的區(qū)別.在那里我發(fā)現(xiàn)了一個(gè)區(qū)別,前者允許空鍵,而后者不允許相同的權(quán)限.就 HashMap 的工作而言,我知道,它在 key 上調(diào)用 hashcode 方法來查找要放置該鍵值對(duì)的存儲(chǔ)桶.我的問題來了:如何計(jì)算空值的哈希碼或空鍵的哈希碼是否有任何默認(rèn)值(如果有,請(qǐng)指定值)?
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);
...
如果你進(jìn)一步看,你會(huì)發(fā)現(xiàn) null 總是進(jìn)入 bin 0
and if you look further you will see that null always goes to bin 0
這篇關(guān)于HashMap 中 NULL 鍵的哈希碼的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!