Search
Calendar
May 2017
S M T W T F S
« Sep    
 123456
78910111213
14151617181920
21222324252627
28293031  
Your widget title
Archives

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

How to fix the Sonar’s following report?

Performance - Inefficient use of keySet iterator instead of entrySet iterator

The report is explicit: replace loop on keySet with a loop on entrySet, which is faster
Eg:
Replace:

for (Integer key : stressList.keySet()){
    Stress stress = stressList.get(key);
}

With:

for (Map.Entry<Integer, Stress>integerStressEntry : stressList.entrySet()){
    Stress stress = integerStressEntry.getValue();
}

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

Leave a Reply