Alex Rivera | Logout

Java generics, nested collection of wildcard

Asked 2011-06-09T13:53:17.630
35

This compiles (1.6)

List<? extends Object> l = new ArrayList<Date>();

But this does not

List<List<? extends Object>> ll = new ArrayList<List<Date>>();

with the error of

Type mismatch: cannot convert from ArrayList<List<Date>> to List<List<? extends Object>>

Could someone explain why? Thanks

EDIT: edited for being consequent

Edit
Report

1 Answer

25

Well the explanations are correct, but I think it'd be a nice thing to add the actual working solution as well ;)

List<? extends List<? extends Object>>

Will work just fine, but obviously the use of such a collection is quite limited by the usual limitations of generic Collections (but then the same is true for the simpler List< ? extends Date >)

answered 2011-06-09T14:06:13.540

Your Answer