Back to Intellij Community

Java8MapForEach

java/java-impl/resources/inspectionDescriptions/Java8MapForEach.html

2025.3-rc-2696 B
Original Source

Suggests replacing for(Entry<?,?> entry : map.entrySet()) {...} or map.entrySet().forEach(entry -> ...) with map.forEach((key, value) -> ...).

Example

void print(Map<String, Integer> map) {
    map.entrySet().forEach(entry -> {
      String str = entry.getKey();
      System.out.println(str + ":" + entry.getValue());
    });
  }

After the quick-fix is applied:

void print(Map<String, Integer> map) {
    map.forEach((str, value) -> System.out.println(str + ":" + value));
  }

When the Do not report loops option is enabled, only entrySet().forEach() cases will be reported. However, the quick-fix action will be available for for-loops as well.

New in 2017.1