Why is SomeClass<? super T> not equivalent to SomeClass in Java generic types?

后端 未结 5 457
清酒与你
清酒与你 2020-12-08 23:52

I noticed the specificaition for Collections.sort:

public static  void sort(List list, Comparator c)

Why

5条回答
  •  囚心锁ツ
    2020-12-09 00:28

    It's obvious to you that, in the case of Comparator, any ancestor of T would work. But the compiler doesn't know that class Comparator functions like that - it just needs to be told whether it should allow or .

    Viewed another way, yes it's true that any Comparator of an ancestor would work in this case - and the way the library developer says that is to use .

提交回复
热议问题