Expected an argument of type Integer, got Object instead
126
127 @Override
128 public void delete(Object key) {
129 map.remove(key);130 }
131
132 @Override
Description
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.
Bad Practice
List<Integer> ints = Arrays.asList(3);
ints.remove("3"); // this will fail silently!
Recommended
Ensure that the type of the value passed to remove()
is the same as the collection's type.
ints.remove((Object)3);
References
- Oracle Java 11 JavaDocs -
java.util.Collection