3

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.

Kinga Odecka
  • 387
  • 4
  • 13
  • See [Mark Peters' answer](http://stackoverflow.com/a/2907411/697449) on the linked question. Make sure to ignore the accepted answer, which is irrelevant. – Paul Bellora Aug 18 '14 at 19:03

0 Answers0