Map
is an object that maps keys to values. A map cannot contain duplicate keys, which means each key can map to at most one value.
When both the key and the value are needed, it is more efficient to iterate the entrySet()
, which will give access to both instead of
iterating over the keySet()
and then getting the value.
If the entrySet()
method is not iterated when both the key and value are needed, it can lead to unnecessary lookups. This is because
each lookup requires two operations: one to retrieve the key and another to retrieve the value. By iterating the entrySet()
method, the
key-value pair can be retrieved in a single operation, which can improve performance.
Noncompliant code example
public void doSomethingWithMap(Map<String,Object> map) {
for (String key : map.keySet()) { // Noncompliant; for each key the value is retrieved
Object value = map.get(key);
// ...
}
}
Compliant solution
public void doSomethingWithMap(Map<String,Object> map) {
for (Map.Entry<String,Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
}