This a a similar question to [FindBugs warning: Inefficient use of keySet iterator instead of entrySet iterator
However, there I am trying to do something a little different. My current code is here:
for (Double key2 : sortedPolygons.keySet()) {
if (sortedPolygons.get(key2).getExteriorRing().equals(hole)) {
sortedPolygons.remove(key2);
break;
}
}
Doing something like the solution in the link does not work. Here is an implementation of said solution:
for(Map.Entry<Double, Polygon> entry : sortedPolygons.entrySet()) {
if (entry.getValue().getExteriorRing().equals(hole)) {
.....
The problem here is that I am trying to delete the entry. There is no entry.remove()
. How can I replace my first block of code, without the FindBugs error:
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.
To note, the underlying structure is TreeMap
, and it cannot be changed.