0

I am trying to understand Java Generics and UpperBound Wildcards on more complicated examples.

I have a list of lists of anything that extends a list of any type of object:

List<List<? extends List<?>>> veryNestedList;

I was thinking whether I the first nested List could actually accept any implementation of ArrayList.

In my thinking:

List<? extends ArrayList<?>> 

should be subType of:

List<? extends List<?>>

So going this way I have tried:

List<List<? extends List<?>>> veryNestedList = new ArrayList<List<? extends ArrayList<?>>>();

Unfortunately this doesn't work.

What I need to do instead is:

List<? extends List<? extends List<?>>> veryNestedList = new ArrayList<List<? extends ArrayList<?>>>();

I'm not really sure whats the reason for this.

@Edit Now when I look at this:

Is it because even though

 List<? extends ArrayList<?>> 

is subtype

 List<? extends List<?>>

When we put it into generic type of another list it's no longer covariant?

Taks
  • 2,033
  • 4
  • 18
  • 23

0 Answers0