star00
V2EX  ›  问与答

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

  •  
  •   star00 · Dec 26, 2019 · 2504 views
    This topic created in 2405 days ago, the information mentioned may be changed or developed.

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

    3 replies    2019-12-26 19:02:59 +08:00
    wysnylc
        1
    wysnylc  
       Dec 26, 2019
    因为 ConcurrentHashMap 的锁机制,get(Object key)如果返回 null,ConcurrentHashMap 没办法判断是 key 不存在还是 value 就是 null
    star00
        2
    star00  
    OP
       Dec 26, 2019
    @wysnylc 网上好多都是这么回答的,但是没有说,不知道有什么影响,即在什么操作时,容易出现什么样的问题。
    findmyself
        3
    findmyself  
       Dec 26, 2019 via iPhone
    Google 下 作者回答过这个问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   965 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 19:20 · PVG 03:20 · LAX 12:20 · JFK 15:20
    ♥ Do have faith in what you're doing.