Use entrySet iterator to fix FindBugs violations, issue #778

These methods accessed the value of a `Map` entry, using a key that was retrieved from a `keySet` iterator. It is more efficient to use an iterator on the `entrySet` of the map, to avoid the `Map.get(key)` lookup.

All violations of FindBugs rule [WMI: Inefficient use of keySet iterator instead of entrySet iterator](http://findbugs.sourceforge.net/bugDescriptions.html#WMI_WRONG_MAP_ITERATOR) are fixed.
This commit is contained in:
Michal Kordas 2015-03-29 18:13:55 +02:00 committed by Roman Ivanov
parent c037e6419d
commit cf1d22f39f
2 changed files with 7 additions and 11 deletions

View File

@ -136,12 +136,10 @@ public class EqualsHashCodeCheck
@Override
public void finishTree(DetailAST rootAST)
{
final Set<DetailAST> equalsDefs = objBlockEquals.keySet();
for (DetailAST objBlock : equalsDefs) {
if (!objBlockWithHashCode.contains(objBlock)) {
final DetailAST equalsAST = objBlockEquals.get(objBlock);
log(equalsAST.getLineNo(), equalsAST.getColumnNo(),
MSG_KEY);
for (Map.Entry<DetailAST, DetailAST> detailASTDetailASTEntry : objBlockEquals.entrySet()) {
if (!objBlockWithHashCode.contains(detailASTDetailASTEntry.getKey())) {
final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY);
}
}

View File

@ -27,7 +27,6 @@ import com.puppycrawl.tools.checkstyle.Utils;
import java.util.BitSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
@ -182,14 +181,13 @@ public class MultipleStringLiteralsCheck extends Check
@Override
public void finishTree(DetailAST rootAST)
{
final Set<String> keys = stringMap.keySet();
for (String key : keys) {
final List<StringInfo> hits = stringMap.get(key);
for (Map.Entry<String, List<StringInfo>> stringListEntry : stringMap.entrySet()) {
final List<StringInfo> hits = stringListEntry.getValue();
if (hits.size() > allowedDuplicates) {
final StringInfo firstFinding = hits.get(0);
final int line = firstFinding.getLine();
final int col = firstFinding.getCol();
log(line, col, MSG_KEY, key, hits.size());
log(line, col, MSG_KEY, stringListEntry.getKey(), hits.size());
}
}
}