0

I'll post a single link here: Collections.sort(). There have been many posts on SO with regards to the PECS paradigm, including this one. In my own personal code, I use generics quite a lot, but have only ever had the use of the P part (that is, <X extends SomethingElse>).

Collections.sort expects as its generics argument a <T extends Comparable<? super T>>. I fail to see where the super kicks in in there. Do you have a concrete example of why this is necessary?

While I am at it, I am deeply afraid that I don't understand the full implications of P either... I have read many, many links, without having had a clear, obvious proof that P is P and C is C...

EDIT As to the may already have an answer here": no, sorry. I have read this link, and many others on SO. This still does not tell me the core mechanism behind it all. The two answers given to me so far give me hints, in fact more so than all the links I could find so far.

Community
  • 1
  • 1
fge
  • 110,072
  • 26
  • 223
  • 312
  • 1
    May be take a look at this - http://stackoverflow.com/q/2723397/1679863 – Rohit Jain Jun 19 '13 at 13:42
  • and [this](http://stackoverflow.com/q/4343202/1679863) and [this](http://stackoverflow.com/q/1368166/1679863) – Rohit Jain Jun 19 '13 at 13:44
  • I _know_ all of these. I am no the wiser. I have read all these links before, without being able to grasp the _real need_. – fge Jun 19 '13 at 13:46
  • Go through [this](http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html) site. This is AFAIK, the best resource on Java Generics. – Rohit Jain Jun 19 '13 at 13:52
  • @RohitJain I also know this site, and I have read the FAQ a number of times. Mrs Langer is a name I know. – fge Jun 19 '13 at 13:55

3 Answers3

2

E.g.

List<Interger> myIntegers = Arrays.asList(5, 2, 3);

List<Long> myLongs = Arrays.asList(5L, 2L, 3L);

MyNumberComparator myNumberComparator = new Comparator<Number>(){...}

Collections.sort(myIntegers, myNumberComparator ); // Number is "super" class of Integer
Collections.sort(myLongs , myNumberComparator ); // Number is "super" class of Long

So "super" here allows to reuse MyNumberComparator for both sorting Integers and sorting Longs.

Puce
  • 34,375
  • 9
  • 72
  • 137
  • 1
    Now we're talking/starting to talk! This is a good example indeed! I'll read that again, find more uses of it, but your example at least expresses _one need_ for `super` clearly. Thanks! – fge Jun 19 '13 at 13:58
2

I fail to see where the super kicks in in there. Do you have a concrete example of why this is necessary?

Say you have a class Animal, which is comparable to all Animals:

public class Animal implements Comparable<Animal>

Now let's say you have a class Dog, which extends Animal:

public class Dog extends Animal

Now the question is, can Dog compare to another Dog? (i.e. can you do myDog.compareTo(yourDog)) The answer is of course. A Dog, as an Animal, is comparable to all Animals, including Dogs (as well as Cats).

However, Dog does not implement Comparable<Dog>, so a bound T extends Comparable<T> would not work for T = Dog.

But for sorting all we care is that the type can compare to itself (which Dog can do). So the suitable least restrictive bound is T extends Comparable<? super T>, which does work for Dog.

newacct
  • 110,405
  • 27
  • 152
  • 217
1

Try to create a List of Number :

List<Number> list = new ArrayList<Number>(Arrays.asList(2, 3.14, 5, 0, 42, 2.5));

Now try to sort it :

Collections.sort(list);

Looking at the generic argument <T extends Comparable<? super T>>, it equivalent in this case to <Number extends Comparable<? super Number>>. However the Number class doesn't implement such a comparator. Hence the code does'nt compile and it forces you to create your own one.

Alexis C.
  • 82,826
  • 18
  • 154
  • 166