How come, in Java, I can write
List<?> list = new LinkedList<Double>();
but not
List<Container<?>> list = new LinkedList<Container<Double>>();
where Container is just something like
public class Container<T> { ... }
This came up because I have a method that accepts a List<Container<?>>
, and I would like to use Arrays.asList to pass arguments to it:
process(Arrays.asList(new Container<Double>(), new Container<Double>()));
But the language doesn't allow this, because it infers the type of Arrays.asList
to be List<Container<Double>>
, and that is not assignable to List<Container<?>>
.
If I add a String-parameterized Container to the call,
process(Arrays.asList(new Container<Double>(), new Container<String>()));
it still doesn't work, because it infers the type List<Container<? extends Serializable & Comparable<?>>>
for Arrays.asList. Only if I pass in something that is neither Comparable nor Serializable does it work properly.
Of course, I can put in a cast and get the compiler to shut up, but I'm wondering if I'm doing something wrong here.