The List interface is the following:
public interface List<E>{
public boolean add(Object e);
public boolean remove(Object e);
public boolean contains(Object e);
...etc
Why aren’t the add, remove and contains methods written like the following?
public boolean add(E e)
public boolean remove(E e)
public boolean contains(E e)
The add method is add(E e), so all is right with the world in that respect.
The remove(Object o) and contains(Object o) methods will operate based on
o.equals(e). This allows you to do some tricky things with special-purpose comparison objects that aren’t necessarily the type of object that are in the collection.Not that this is always recommended, or even a best practice. But it is a neat trick.