RFR: 8282178: Replace simple iterations of Map.entrySet with Map.forEach calls

liach duke at openjdk.java.net
Wed Feb 23 22:42:22 UTC 2022


Replaces simple `for (Map.Entry<K, V> entry : map.entrySet())` with `map.forEach((k, v) ->)` calls. This change is better for thread-safety and reduces allocation for some map implementations.

A more in-depth description of benefits is available at https://mail.openjdk.java.net/pipermail/core-libs-dev/2022-February/086201.html and at the JBS issue itself.

A [jmh comparison](https://jmh.morethan.io/?sources=https://gist.githubusercontent.com/liach/0c0f79f0c0b9b78f474d65cda2c5f7b5/raw/4f2a160c51164aefdfac6ab5a19bdbc8c65f5fcf/base-results.json,https://gist.githubusercontent.com/liach/0c0f79f0c0b9b78f474d65cda2c5f7b5/raw/4f2a160c51164aefdfac6ab5a19bdbc8c65f5fcf/head-results.json) on the performance of the existing `HashMapBench` shows that the performance of `putAll` for `HashMap` has not significantly changed.

-------------

Commit messages:
 - Merge branch 'master' of https://git.openjdk.java.net/jdk into map-foreach
 - Fix initialization issues
 - 8282178: Replace simple iterations of Map.entrySet with Map.forEach calls

Changes: https://git.openjdk.java.net/jdk/pull/7601/files
 Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=7601&range=00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8282178
  Stats: 47 lines in 7 files changed: 26 ins; 6 del; 15 mod
  Patch: https://git.openjdk.java.net/jdk/pull/7601.diff
  Fetch: git fetch https://git.openjdk.java.net/jdk pull/7601/head:pull/7601

PR: https://git.openjdk.java.net/jdk/pull/7601


More information about the core-libs-dev mailing list