I'm trying to proove on example that each fragment in Collections.max
declaration is necessary to achieve maximum flexibility.
In what situation, bound on Collection
wildcard is necessary? (signature simplified)
public <T extends Comparable<? super T>> T max(Collection<? extends T> coll)
versus
public <T extends Comparable<? super T>> T max(Collection<T> coll)
I know about PECS rule, but every example I can think of works the same with second declaration and I am looking for example to proove that second extends
i necessary.
Someone proposed the answer that max()
must accept Collection<? extends Something>
but it accepts even without questionable bound:
public static class Animal implements Comparable<Animal> {
Created collection from response and passed it to extends
-less method:
public static <T extends Comparable<? super T>> T max(Collection<T> coll) {...}
...
Collection<? extends Animal> someAnimalCollection = new ArrayList<Animal>();
Animal u = max(someAnimalCollection);
No errors, no warnings.