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

后端 未结 5 1062
野趣味
野趣味 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条回答
  •  旧时难觅i
    2021-01-05 20:49

    Types in methods calls can be inferred.

    Xyz[] a;
    Arrays.sort(a, Collections.reverseOrder());
    

    is equivalent to

    Xyz[] a;
    Arrays.sort(a, Collections.reverseOrder());
    

    From Java SE 7 something similar works for constructors, only you need to use the "diamond 'operator'" (for not very good reasons).

    List xyzs = new ArrayList<>();
    

提交回复
热议问题