ArrayList described as
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
And most of main methods work with E generic type (get, set, add, addAll etc).
But methods contains, indexOf, lastIndexOf and remove take the Object type as parameter - is this only because of inner use or Object.equals() or anything else?
The collection types only use their generic type E
in cases where it must match in order for the operation to be legal. If you have a list of strings then clearly something like add
can only make sense when passed a string.
But for operations like contains
it's different - it is perfectly legal to ask whether a list of strings contains a particular Integer
, for example. The answer will always be no, but that doesn't make it an error to ask the question.