to List> " It seems that the “cherry ...">

Is the "cherry cat list" a "coke list"?

I get "Type mismatch: cannot convert from List<CherryCoke> to List<Coke<?>> "

It seems that the “cherry cat list” is not a “coke list”. This is controversial. How can I create this "xs" anyway if it should be List<Coke<?>> and I should have a subclass of Coke<Cherry> ?

 class Taste { } class Cherry extends Taste { } abstract class Coke<T extends Taste> { } class CherryCoke extends Coke<Cherry> { } class x { void drink() { List<Coke<?>> xs = Arrays.asList(new CherryCoke()); } } 
+6
source share
3 answers

You are right - the “list of coke” is not the “list of cherry cats”, but the list of “things that expand the coke” is the “list of cherry cokes”.

You probably want to define xs as List <? extends Coke<?>> xs = Arrays.asList(new CherryCoke()); List <? extends Coke<?>> xs = Arrays.asList(new CherryCoke());

+9
source

As others have noted, List<CherryCoke> and List<Coke<?>> are not the same thing.

But it's not a problem. No need to type xs as List<? extends Coke<?>> List<? extends Coke<?>> . You could just create a new List<Coke<?>> and then add a new CherryCoke to it.

The error is simply due to the fact that the compiler incorrectly deduced the asList type T parameter. If you specify T yourself, it will compile:

 List<Coke<?>> xs = Arrays.<Coke<?>>asList(new CherryCoke()); 
+5
source
 List<Coke<?>> xs = Arrays.asList(new CherryCoke()); 

List<CherryCoke> not a subtype of List<Coke<?>> (List of Coke of anything).

you must define the top of your generic type.

 List<? extends Coke<?>> xs = Arrays.asList(new CherryCoke()); 
+4
source

All Articles