Hashcode for NULL key in HashMap(HashMap 中 NULL 键的哈希码)
问题描述
我只是在阅读 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 键的哈希码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:HashMap 中 NULL 键的哈希码


基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01