The java.util.Collection
type and its subtypes provide methods to access and modify collections such as Collection.remove(Object
o)
and Collection.contains(Object o)
. Some of these methods accept arguments of type java.lang.Object
and will
compare said argument with objects already in the collection.
If the actual type of the argument is unrelated to the type of object contained in the collection, these methods will always return
false
, null
, or -1
. This behavior is most likely unintended and can be indicative of a design issue.
This rule raises an issue when the type of the argument provided to one of the following methods is unrelated to the type used for the collection
declaration:
-
Collection.remove(Object o)
-
Collection.removeAll(Collection<?>)
-
Collection.contains(Object o)
-
List.indexOf(Object o)
-
List.lastIndexOf(Object o)
-
Map.containsKey(Object key)
-
Map.containsValue(Object value)
-
Map.get(Object key)
-
Map.getOrDefault(Object key, V defaultValue)
-
Map.remove(Object key)
-
Map.remove(Object key, Object value)