use ConcurrentHashMap to replace Collections.synchronizedMap(HashMap) ?

David Holmes david.holmes at oracle.com
Thu Apr 21 11:22:22 UTC 2022


On 21/04/2022 5:09 am, Roger Riggs wrote:
> Hi,
> 
> There's usually a 'it depends' in questions like this.
> 
> ConcurrentHashMap *does NOT allow null *keys or values. HashMap *does 
> allow null* as keys and values.
> 
> And more obviously, using a synchronized HashMap does not allow any 
> concurrency.
> ConcurrentHashMap is built to allow concurrent access.

CHM also doesn't allow synchronized external iteration.

David

> Regards, Roger
> 
> On 20/04/2022 14:26, Xeno Amess wrote:
>> Collections.synchronizedMap(new HashMap<Integer, byte[]>())
>>
>> new ConcurrentHashMap <Integer, byte[]>  ()
>>
>> Is the later one always better than the first one?
>>
>> I personally think so, but are there any traps in it?


More information about the jdk-dev mailing list