In ArrayList API add() takes an argument of generic parameter type, but contains() and indexOf() take arguments of type Object.
public class ArrayList<E> ...{
public boolean add(E e);
public boolean contains(Object o);
public int indexOf(Object o);
....
}
So i am just wondering if its something to do with Generics or it’s design in-consistency ?
I looked at Openjdk implementation but couldn’t find any specific reason for this.
What the API is saying is that:
add()anything that isn’t anE;E(but that could compare equal to an instance ofE).Consider the following example:
The last three lines wouldn’t compile if
contains()were restricted to takingData.