HashMap中的NULL键的Hashcode

HashMap中的NULL键的Hashcode

本文介绍了HashMap中的NULL键的Hashcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是看看Java中的HashMap和HashTable类之间的区别。在那里我发现一个区别,前者允许null键和后来没有特权的相同。
就HashMap的工作而言,我知道,它会调用hashcode方法来查找要放置该键值对的桶。这里提出了我的问题:
如何计算空值的哈希码,或者是否有null键的哈希码的默认值(如果是这样,请指定值)?

  public V put(K key,V value){ 
if(key == null)
return putForNullKey(value);
...

如果你看得更远,你会看到null总是去bin 0


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:

public V put(K key, V value) {
   if (key == null)
      return putForNullKey(value);
   ...

and if you look further you will see that null always goes to bin 0

这篇关于HashMap中的NULL键的Hashcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 14:18