How does Java 8 know which String::compareTo method reference to use when sorting?

后端 未结 2 1344
北恋
北恋 2021-01-02 10:05

How does Java know which String::compareTo method reference to use when calling Collections.sort(someListOfStrings, String::compareTo);? comp

2条回答
  •  孤城傲影
    2021-01-02 10:23

    OK, the source of Collections.sort() looks as follows:

    public static  void sort(List list, Comparator c) {
       Object[] a = list.toArray();
       Arrays.sort(a, (Comparator)c);
       ListIterator i = list.listIterator();
       for (int j=0; j

    I think it is quite clear now. The contents is a list. It means it has an order and the items are treated one by one in that order.

提交回复
热议问题