Search
Calendar
June 2025
S M T W T F S
« May    
1234567
891011121314
15161718192021
22232425262728
2930  
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