Possible Duplicate:
Why aren’t Java Collections remove methods generic?
The Java Collection<E> interface has a contains method with the following signature:
boolean contains(Object o)
Since the interface is generic with type E shouldn’t the signature be
boolean contains(E o)
to reflect that and only allow arguments of type E.
The same question can be asked regarding the remove(Object o) method
The
containsandremovemethods accept any object because the they accept (and can succeed with) objects that might not be instances ofE. The contract forcontainsis:Note that
oneed not actually be an object in the collection; it must merely pass theequalstest.The same idea goes for
remove.See also this thread, where it is pointed out that making
containsandremovegeneric would have broken a lot of existing, perfectly valid code.