在通用 class 中为通配符传递任何具体 class 会产生错误。为什么?

Passing any concrete class for a wildcard in a generic class gives error. Why?

我有一种方法可以使用 Integer 键和任何类型的值对复合 Entry 对象列表进行排序。它具有以下签名:

static void bucketSort(List<Entry<Integer, ?>> list, int n)
{
    //some code
}

其中进入界面如下:

interface Entry<K, V> {
    K getKey(); //returns key stored in this entry
    V getValue(); //returns the value stored in this entry
}

然而,即使在我的理解中任何对象都可以传递给“?”,这段代码在第二行产生了一个编译错误("cannot resolve method"):

List<Entry<Integer, String>> list = new ArrayList<>();
bucketSort(list, 100);

此外,这段代码没有报错:

List<Entry<Integer, ?>> list = new ArrayList<>();
bucketSort(list, 100);

谁能帮我理解这是为什么?另外,推荐的解决问题的方法是什么?

也对 Entry 对象使用通配符,通配​​符表示未知类型。在上面的代码中,您使用键 Integer 和值 unknown type 定义了 Entry<Integer, ?> 条目,但是对于 List

中的条目对象,您必须采用相同的方式
static void bucketSort(List<? extends Entry<Integer, ?>> list, int n)
{
    //some code
}