In my opinion it should work, but it does not. Why? Source code:
package javaapplication1;
import java.util.*;
class A
{
public static <K,V> Map<K,V> map()
{
return new HashMap<K,V>();
}
}
class Person {}
class Dog {}
public class JavaApplication1
{
static void f(Map<Person, List<? extends Dog>> peopleDogList) {}
public static void main(String[] args)
{
f(A.<Person, List<Dog>>map());
}
}
Very simple code. Compiler error:
method f in class JavaApplication1 cannot be applied to give types;
required: Map<Person, List<? extends Dog>
found: Map<Person, List<Dog>>
reason: actual arguments Map<Person, List<Dog>>
cannot be converted to Map<Person, List<? extends Dog>
by method invocation conversion.
Map<Person, List<? extends Dog>
is more general, so the compiler should be able to convert?
Also this: Map<Person, List<? extends Dog>> peopleDogList = A.<Person, List<Dog>>map();
does not work. ? extends Dog
means object that inherits Dog
or Dog
, so word Dog
should be ok?
> messages3 = new ArrayList>();` does not?