Please refer to the following method :
public Set<LIMSGridCell> getCellsInColumn(String columnIndex){
Map<String,LIMSGridCell> cellsMap = getCellsMap();
Set<LIMSGridCell> cells = new HashSet<LIMSGridCell>();
Set<String> keySet = cellsMap.keySet();
for(String key: keySet){
if(key.startsWith(columnIndex)){
cells.add(cellsMap.get(key));
}
}
return cells;
}
FindBugs give this waring message :
"Inefficient use of keySet iterator instead of entrySet iterator This method accesses 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."
You are retrieving all the keys (accessing the whole map) and then for some keys, you access the map again to get the value.
You can iterate over the map to get map entries (Map.Entry) (couples of keys and values) and access the map only once.
Map.entrySet() delivers a set of Map.Entry
s each one with the key and corresponding value.
for ( Map.Entry< String, LIMSGridCell > entry : cellsMap.entrySet() ) {
if ( entry.getKey().startsWith( columnIndex ) ) {
cells.add( entry.getValue() );
}
}
Note: I doubt that this will be much of an improvement since if you use map entries you will instantiate an object for each entry. I don't know if this is really faster than calling get()
and retrieving the needed reference directly.