Recent Posts
Archives

PostHeaderIcon Sonar / Inefficient use of keySet iterator instead of entrySet iterator

How to fix the Sonar’s following report?
[java]Performance – Inefficient use of keySet iterator instead of entrySet iterator[/java]

The report is explicit: replace loop on keySet with a loop on entrySet, which is faster
Eg:
Replace:
[java]for (Integer key : stressList.keySet()){
Stress stress = stressList.get(key);
}[/java]
With:
[java]for (Map.Entry<Integer, Stress>integerStressEntry : stressList.entrySet()){
Stress stress = integerStressEntry.getValue();
}[/java]

Yet, if you need only keys or only values of your Map, then use rather keySet() or values().

Leave a Reply