• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

HashMap NULL 键的哈希码

用户头像
it1352
帮助1

问题说明

我只是在阅读 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)?

正确答案

#1

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangicaak
系列文章
更多 icon
同类精品
更多 icon
继续加载