I am trying to learn Java generics. I am not clear when you will use <T extends Foo> and when you will use <T super Foo>. What do each one of these things mean about T? Lets say I have <T extends Comparable> and <T super Comparable>, what do each of these mean?
I’ve read several tutorials at sun.com but I’m still lost. Can somebody illustrate with examples?
See Effective Java 2nd Edition, Item 28:
PECS
Producer extends, Consumer super
If your parameter is a producer, it should be
<? extends T>, if it’s a consumer it has to be<? super T>.Take a look at the Google Collections, they know how to use it, because they got Bloch 😉