HashMap<Integer, ArrayList<Integer>> cityMap = new HashMap<>();
...
for (ArrayList<Integer> list : cityMap.values()) {
int size = list.size();
if (size > 0) {
list.removeIf(i -> true);
}
}
I don't quite understand what the removeIf
does in this case. Especially the part (i -> true
). Thank you for any explanation.
The Javadoc of
removeIf()
states:The predicate in your example is always
true
because you map each integeri
in your list totrue
by the expression:i -> true
.I added a simpler example which removes all even integers and keeps all odd integers by the predicate
i % 2 == 0
:Ugly setup:
Keep only odd numbers:
Output:
removeIf
will go through each element in your list and run the specified predicate (boolean function) on it. If the predicate returnstrue
, it will be removed from the list. If the predicate returnsfalse
, it will not.In your case, every element will result in the predicate returning
true
, thus clearing the list.