This has sort of been discussed just recently, for example here.
The documentation states that CHM
provide weakly consistent traversal, as opposed to fail-fast traversal; as seen here What this means:
they may proceed concurrently with other operations
they are guaranteed to traverse elements as they existed upon construction exactly once, and may (but are not guaranteed to) reflect any modifications subsequent to construction.
Generally CHM
operations that change it's structure are thread-safe, but what you want to achieve is still discouraged by the Stream documentation. This is called interference and should be avoided. If you change that structure with an simple HashMap
you will get ConcurrentModificationException
since you are modifying the source while traversing it. Unless you have a compelling reason not to discard your previous map, you should just filter the values you are interested in and collect those.
You can also see how you can get un-expected results when you add to a CMH
from the link above.