1
List<? extends someObject> list
List<? extends Comparable<? super T>> list

I cannot understand the second code. As i understand from the first code,it stores any object that extends some object but in the second code does that mean it stores interface?

Lii
  • 11,553
  • 8
  • 64
  • 88
  • Maybe: http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java –  Feb 10 '16 at 06:36
  • 1
    The second list store any objects that implement the Comparable interface – Martheen Feb 10 '16 at 06:37

1 Answers1

0

The first is a list of somethings that are at least SomeObject. It might be the case that you can not insert SomeObject in the list, if it is a list of a true subclass. Look at it this way:

List<? extends Animal> list

This is a list of somethings that are at least Animal. It might be the case, that this is a list of cats, or a list of dogs. We don't know, hence the ?. In any case, we know we can get at leastAnimals out of the list, but we can not insert because we don't know the exact type in the list.

The second is a list of comparable implementations (or derived interfaces) that apply to T or some superclass. All objects in this list can therefore be used to compare T objects. But of course, same as before, we can not be sure what exact type of Comparable the list uses as items, so we can not add anything, we can only read it in this form.

Edit: expanding on Comparable<? super T>: It may seem counter-intuitive to use super instead of extends. It is because if you want to compare two things, you can do it by comparing the thing itself or any higher category of the thing. So, if I want to compare dogs, it is enough for me to know how to compare animals. If I can compare animals I can compare any animal, including dogs, cats, etc. If I however only know how to compare cats I might not know how to compare dogs. So if you want to find somebody to compare T, you have to look for somebody who can compare T, or any superclass of T.

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38