The idea is that ConcurrentHashMap
uses buckets, like any other hash-based structure. When you lookup (remove
) an entry, it uses the hashCode to find that bucket where the entry might reside first and then remove it immediately.
The question is, will you be able to spot/see this removal that actually happened; for example you iterate and print the contents of CHM
and remove some of the entries at the same time. If you already "visited" some bucket via forEach
for example (and traversal happens on bucket-to-bucket basis), and the entry was removed from that bucket - you will not spot it.
On the other hand if you will remove and entry from a "yet to be visited" bucket - you will spot the removal.
That is for example the reason that size
returns a "at the moment" known size - it counts for example entries from bucketA, then later someone remove some of those entries, CHM
will not get back or synchronize to count those again.
Also some very good comments to read here