Arguments to collection remove*
methods must be of the same type as the collection itself.
Though Collection
is parameterised on the type of the contained values, Collection.remove()
is not; it accepts a parameter of type Object
instead. This means any value can be passed to a collection's remove()
method, regardless of whether the value's type matches the collection's type.
This is also exacerbated for lists that store integers; List
has both an Object
and an int
overload for remove()
that are easy to confuse.
List<Integer> ints = Arrays.asList(3);
ints.remove("3"); // this will fail silently!
Ensure that the type of the value passed to remove()
is the same as the collection's type.
ints.remove((Object)3);
java.util.Collection