Skip to main content

Iterate a Map's entrySet() rather than its keySet()

org.openrewrite.staticanalysis.UseMapEntrySetIteration

A loop over map.keySet() that calls map.get(key) hashes and probes the map again for every element, which on a TreeMap costs an extra O(log n) lookup per iteration. Iterating map.entrySet() instead hands the loop both the key and the value. The loop is only rewritten when:

  • The map is a simple reference that is neither modified nor reassigned inside the loop.
  • get is called only with the loop variable.
  • The loop variable is neither reassigned nor captured by a lambda or anonymous class.

Every candidate loop, converted or not, is recorded in a data table along with the reason it was left alone.

Tags

Recipe source

GitHub: UseMapEntrySetIteration.java, Issue Tracker, Code Genome Project

This recipe is available under the Moderne Source Available License. Moderne customers can download precompiled artifacts from The Code Genome Project. For non-commercial use you can build the artifact from source locally.

Example

Before
import java.util.Map;

class Test {
void test(Map<String, Integer> map) {
for (String key : map.keySet()) {
Integer w = map.get(key);
System.out.println(key + "=" + w);
}
}
}
After
import java.util.Map;

class Test {
void test(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
Integer w = entry.getValue();
System.out.println(entry.getKey() + "=" + w);
}
}
}

Usage

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-static-analysis` in your build file or by running a shell command (in which case no build changes are needed):

OpenRewrite artifacts are distributed through the Code Genome Project repository (https://artifacts.codegenomeproject.org/maven), which requires authentication. Sign in to the Code Genome Project to create a download token, then in the snippets below replace USERNAME with the email or username you signed in with and TOKEN with that token. See the quickstart guide for details.

  1. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.staticanalysis.UseMapEntrySetIteration")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    maven {
    url = "https://artifacts.codegenomeproject.org/maven"
    credentials {
    username = "USERNAME"
    password = "TOKEN"
    }
    }
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.42.0")
    }
  2. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables

Map keySet() iterations

org.openrewrite.staticanalysis.table.MapKeySetIterations

Loops that iterate a map's keySet() and look the value up again with get(key), and whether they were converted to entrySet() iteration.

Column NameDescription
Source pathThe path to the source file containing the loop.
ClassThe fully qualified name of the class containing the loop.
Map expressionThe expression the keySet() and get(key) calls are made on.
UpdatedWhether the loop was rewritten to iterate entrySet().
ReasonWhy the loop was left unchanged. Empty when the loop was updated.