为何Hashtable ConcurrentHashmap不允许key 和 value为null
所属分类 java
浏览量 1568
集合名称 key value 说明
HashMap 允许为null 允许为null 线程不安全
TreeMap 不允许为null 允许为null 线程不安全
HashTable 不允许为null 不允许为null 线程安全
ConcurrentHashMap 不允许为null 不允许为null 线程安全
The main reason that nulls aren’t allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps)
is that ambiguities that may be just barely tolerable in non-concurrent maps can’t be accommodated.
The main one is that if map.get(key) returns null, you can’t detect whether the key explicitly maps to null vs the key isn’t mapped.
In a non-concurrent map, you can check this via map.contains(key),
but in a concurrent one, the map might have changed between calls.
ConcurrentHashmap 和 Hashtable 支持并发,通过get(k)获取对应的value时,如果获取到的是null时,无法判断value为null,还是key不存在。
HashMap是非并发的,可以通过contains(key)来做判断。支持并发的Map在调用m.contains(key)和m.get(key) 值可能已经变了。
If the map can hold nulls, there is no way to tell get is returning a null
because there was no key for that value, or just because the value was null.
https://stackoverflow.com/questions/698638/why-does-concurrenthashmap-prevent-null-keys-and-values
上一篇
下一篇
应用部署策略
aerospike write-block-size 参数说明
kubernetes核心概念
prometheus VS influxdb
go语言历程
go语言特性及优点