Files
codeql/java/ql/src/Performance/InefficientKeySetIterator.java
2018-08-30 10:48:05 +01:00

25 lines
696 B
Java

// AVOID: Iterate the map using the key set.
class AddressBook {
private Map<String, Person> people = ...;
public String findId(String first, String last) {
for (String id : people.keySet()) {
Person p = people.get(id);
if (first.equals(p.firstName()) && last.equals(p.lastName()))
return id;
}
return null;
}
}
// GOOD: Iterate the map using the entry set.
class AddressBook {
private Map<String, Person> people = ...;
public String findId(String first, String last) {
for (Entry<String, Person> entry: people.entrySet()) {
Person p = entry.getValue();
if (first.equals(p.firstName()) && last.equals(p.lastName()))
return entry.getKey();
}
return null;
}
}