Hello, In the patch for 8281631 <https://bugs.openjdk.java.net/browse/JDK-8281631>, xenoamess intentionally avoided <https://github.com/openjdk/jdk/pull/7431#discussion_r810666916> repeatedly calling Map::size, for it may not be constant-timed due to being concurrent. This alerted me that the for loops on Map::entrySet may be similarly not thread-safe.
I believe that we should migrate for loop on Map::entrySet to Map::forEach calls; concurrent map callees usually have dedicated logic <https://github.com/openjdk/jdk/blob/2557ef8a02fe19784bd5e605b11d6bd574cde2c2/src/java.base/share/classes/java/util/concurrent/ConcurrentMap.java#L119> to mitigate such thread-safety issues. Synchronized map <https://github.com/openjdk/jdk/blob/2557ef8a02fe19784bd5e605b11d6bd574cde2c2/src/java.base/share/classes/java/util/Collections.java#L2735> callees are benefitted too. Another lesser benefit is reduced object allocation for iteration. While the most popular implementations (HashMap, LinkedHashMap, WeakHashMap, TreeMap) don't need to allocate entries for iteration, many other (EnumMap, IdentityHashMap, concurrent maps) do, and iterating those maps with forEach is less costly. (Note that 8170826 <https://bugs.openjdk.java.net/browse/JDK-8170826> takes care of implementing proper forEach in EnumMap) A JBS issue has already been submitted at https://bugs.openjdk.java.net/browse/JDK-8282178, and I have prepared a patch. This patch modifies the putAll implementations of a few JDK maps to let the callee feed entries through Map::forEach to be put into the maps. Two places of Map::entrySet calls in Collectors has also been updated. For most other existing usages in the JDK, I don't think they will benefit from such a migration: They mostly iterate on the entryset of the popular implementations that already host entries and are single-threaded, and I don't think it's in our best interest to touch those use cases. So, here are my questions: 1. Is such a concept of replacing Map::entrySet calls with Map::forEach calls reasonable, or is there any fundamental flaw? If the concept sounds good, I will submit a patch, so we can answer 2. Is the changes implemented correct for this concept, i.e. targeting code where users supply callee maps? Best regards