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()
.