3

我定义了这个方法:

public static <E extends Comparable<E>> NavigableSet<E> navigableSet (List<E> l)

当我写:

NavigableSet<Integer> set = Sets.navigableSet(new ArrayList<Integer>());

一切都很好,但如果我写这个:

NavigableSet<Integer> set = Sets.navigableSet(new ArrayList<>());

Java 无法自动推断 Integer 类型。我收到以下错误:

error: no suitable method found for navigableSet(ArrayList<Object>)
NavigableSet<Integer> set = Sets.navigableSet(new ArrayList<>());
                                ^
method Sets.<E#1>navigableSet(List<E#1>) is not applicable
  (inferred type does not conform to equality constraint(s)
    inferred: E#2
    equality constraints(s): E#3)

为什么?是什么阻止了 Java 做出正确的推理?

我的Java版本是:

openjdk 11.0.5 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)
4

0 回答 0