How does Collections.reverseOrder know what type parameter to use while returning Comparator

后端 未结 5 1066
野趣味
野趣味 2021-01-05 20:02

As per Java API spec, the signature of Collections.reverseOrder is

public static Comparator reverseOrder()

And the example gi

5条回答
  •  甜味超标
    2021-01-05 20:53

    Arrays.sort() knows what kind of Comparator it needs, since T is specified by the first argument (a):

    public static  void sort(T[] a, Comparator c)
    

    EDIT:

    @Louis Wasserman correctly points out that we only need a Comparator, not a Comparator. Since Object is a superclass of any T, then Comparator (the default if no generic parameters are given) is sufficient.

    提交回复
    热议问题