众所周知,在Java中Map可以存储null,而ConcurrentHashMap不能存储null值,那么为什么呢?
一、先出源码出发
put方法点进去~
@throws NullPointerException if the specified key or value is null and this map does not permit null keys or values
可以清晰的看到源码中规定,ConcurrentHashMap是不可以存储null值的。
二、那么究竟这是为什么呢?
可以先具体的了解一下ConcurrentHashMap。
与HashMap一样,ConcurrentHashMap也是一个基于散列的Map,但它使用了一种完全不同的加锁策略来提供更高的并发性和伸缩性。ConcurrentHashMap并