Java

Java

By DeepSource

Inefficient use of keySet iterator instead of entrySet iterator JAVA-P0361

Performance

This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator.

It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup.

Bad Practice

for (String key: map.keySet()) {
    ...
    if (satisfiesCriteria(key))
        value = map.get(key); // Inefficient
    ...
}

Recommended

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    ...
    if (satisfiesCriteria(entry.getKey())
        value = entry.getValue();
    ...
}

While the performance benefits of this change may not be very high for smaller maps, it is worth making this change if you will be handling maps with very large capacities (entry count in the millions for example), and/or slower or bad hashing implementations.

References