Search code examples
javawildcardnested-generics

Bounded wilcard not working for List inserted in Map


The following code explains my problem:

interface f1 {}

interface f2 extends f1{}


1. List<? extends f1> l1 = new ArrayList<f2>();

2. Map<String, ? extends f1> m1 = new HashMap<String, f2>();

3. Map<String, List<? extends f1>> m2 = new HashMap<String, List<f2>>();

No. 1 and 2 work but no. 3 gives an error on eclipse. It says: Type mismatch: cannot convert from HashMap<String,List<f2>> to Map<String,List<? extends f1>> Could you please help me figure out why.


Solution

  • Use

    Map<String, ? extends List<? extends f1>> m2 = new HashMap<String, List<f2>>();