HashMap<String, ArrayList<? extends Serializable>> map = new HashMap<String, ArrayList<ArrayList>>();
This does not compile. To the best of my knowledge on Java Generics, it should. And this:
ArrayList<? extends Serializable> c = new ArrayList<ArrayList<String>>();
successfully compiles.
Can anyone say why the above wouldn't compile?
Why do you think that it should? A HashMap<String, Apple>
is never assignable from a HashMap<String, Orange>
for any possible unequal Apple
and Orange
, as long as both Apple
and Orange
are not wildcard-types.
And ArrayList<? extends Serializable>
is not the same thing as ArrayList<ArrayList<?>>
.
What you probably meant:
HashMap<String, ? extends ArrayList<? extends Serializable>> map =
new HashMap<String, ArrayList<ArrayList<?>>>();
Now it compiles, because indeed:
? extends Serializable // can be assigned from
ArrayList<?>
and
? extends ArrayList<? extends Serializable> // can be assigned from
ArrayList< ArrayList<?>>