In this example:
import java.util.*;
public class Example {
static void doesntCompile(Map<Integer, List<? extends Number>> map) {}
static <T extends Number> void compiles(Map<Integer, List<T>> map) {}
static void function(List<? extends Number> outer)
{
doesntCompile(new HashMap<Integer, List<Integer>>());
compiles(new HashMap<Integer, List<Integer>>());
}
}
doesntCompile()
fails to compile with:
Example.java:9: error: incompatible types: HashMap<Integer,List<Integer>> cannot be converted to Map<Integer,List<? extends Number>>
doesntCompile(new HashMap<Integer, List<Integer>>());
^
while compiles()
is accepted by the compiler.
This answer explains that the only difference is that unlike <? ...>
, <T ...>
lets you reference the type later, which doesn't seem to be the case.
What is the difference between <? extends Number>
and <T extends Number>
in this case and why doesn't the first compile?
>](https://stackoverflow.com/questions/746089/java-generic-listlist-extends-number)
– Mạnh Quyết Nguyễn Mar 08 '20 at 01:04