Why is the producer in Collections.max () a bounded pattern?

I am trying to create an example so that every chunk in a declaration is Collections.max

needed for maximum flexibility.

In what situation is template binding needed Collection

?
(signature simplified)

public <T extends Comparable<? super T>> T max(Collection<? extends T> coll)

against

public <T extends Comparable<? super T>> T max(Collection<T> coll)

I am aware of the PECS rule, but every example I can think of works the same with the second declaration, and I am looking for an example in order to create the second extends

I needed.

Someone suggested an answer that he max()

should accept Collection<? extends Something>

, but he accepts even without a dubious binding:

public static class Animal implements Comparable<Animal> {

Created a collection from the answer and passed it to the 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.

+3


source to share





All Articles