-1

The two seem to give the same result. What am I missing? Thanks.

List<List<?>>
    <==> List<List<? extends Object>>
    <==> List<List<Number>>  **(for example)**

List<? extends List<?>>
    <==> List<? extends List<? extends Object>>
    <==> List<? extends List<Number>>  **(for example)**
    <==> List<List<Number>>  **(List<Number> is a subtype of ? extends List<Number>**
adeo
  • 33
  • 1
  • 3
  • Hint - try passing an object of type `List>` into a method that takes a parameter of type `List>`. Then try passing the same type into a method that takes a parameter of type `List extends List>>`. – Michael Berry Jun 15 '20 at 11:43
  • Try it; what types can you successfully add to that second list? – Joe Jun 15 '20 at 11:45

1 Answers1

0

In the first case, you have a list of a list of something.

In the second case, you have a list of something that extends a list of something.

In your example, the second part of code works because, indeed, a List<number> , in it's own way, extends List<?>. You can read more about polymorphism to learn why.

Bellow is an example on what you can do with that.

class MyList<T> extends List<T> {}

List<List<?>> someVariable = Arrays.asList(new MyList(), new MyList()); // won't work.

List<? extends List<?>> someOtherVariable = Arrays.asList(new MyList(), new MyList()); // will work.

This is a very basic example but it shows the difference between the two.

Nicolas
  • 8,077
  • 4
  • 21
  • 51