V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
star00
V2EX  ›  问与答

ConcurrentHashMap 的 key, value 为什么不能为 null?

  •  
  •   star00 · 2019-12-26 17:15:04 +08:00 · 1787 次点击
    这是一个创建于 1580 天前的主题,其中的信息可能已经有所发展或是发生改变。

    众所周知,ConcurrentHashMap 的 key, value 不能为 null。但是为什么不能为空呢。是为了确保它是线程安全,必须要这么设计吗? 多谢大佬指点

    3 条回复    2019-12-26 19:02:59 +08:00
    wysnylc
        1
    wysnylc  
       2019-12-26 18:01:42 +08:00
    因为 ConcurrentHashMap 的锁机制,get(Object key)如果返回 null,ConcurrentHashMap 没办法判断是 key 不存在还是 value 就是 null
    star00
        2
    star00  
    OP
       2019-12-26 18:53:29 +08:00
    @wysnylc 网上好多都是这么回答的,但是没有说,不知道有什么影响,即在什么操作时,容易出现什么样的问题。
    findmyself
        3
    findmyself  
       2019-12-26 19:02:59 +08:00 via iPhone
    Google 下 作者回答过这个问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1027 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:42 · PVG 03:42 · LAX 12:42 · JFK 15:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.