I’m starting to learn Java and I have a question about generics.
In this methods from Collection<E> interface:
boolean containsAll( Collection <?> c);
boolean removeAll(Collection<?> c);
boolean retainAll ( Collection <?> c);
Why is the parameter Collection <?> c instead of Collection <E> c?
Thanks a lot
The JDK designers wanted code like the following to be possible:
(The above code might not exactly compile because I can’t remember how
Arrays.asList()captures type parameters, but it should get the point across.)That is, because you can call
.equals()on any pair of objects and get a meaningful result, you don’t really need to restrict those methods to a specific item type.