I have a HashMap<ArrayList<Integer>, Integer>
. I'm removing objects from the Map before replacing them, so I'm trying to avoid put
calls which already have the key present in order to keep the Map size the same (for a steady-state Evolutionary Algorithm I'm writing from scratch).
The replacements are coming from an ArrayList<ArrayList<Integer>> listOfArrs
. Throughout some of my runs, the Map size has been decreasing because there have been duplicates present in this nested ArrayList. In the course of debugging, I've found this problem: the following code prints GOODBYE
but not HELLO
.
void updateMap(ArrayList<ArrayList<Integer>> listOfArrs) {
//debug loop
for (ArrayList<Integer> arr : listOfArrs) {
if (mapObj.containsKey(arr)) System.out.println("HELLO");
}
for (ArrayList<Integer> arr : listOfArrs) {
if (mapObj.containsKey(arr)) System.out.println("GOODBYE");
mapObj.put(arr, 0);
}
}
When I originally get the listOfArrs
from a different function, I use listOfArrs.removeIf(arr -> mapObj.containsKey(arr))
in an attempt to prevent duplicates, but it seems that's not working.
Figured it out - it's an obvious one. The listOfArrs contained duplicates that I didn't check for. When I'm iterating through that second for loop in the original post they start showing up because I'm checking the map after I've added one of the first duplicates.