How does the sort() method of the Collection class call the Comparable's compareTo()?

有些话、适合烂在心里 提交于 2019-12-01 06:23:51

Please see open jdk source codes. I guess it helps.

java.util.Collections:

132     public static <T extends Comparable<? super T>> void More     ...sort(List<T> list) {
133         Object[] a = list.toArray();
134         Arrays.sort(a);
135         ListIterator<T> i = list.listIterator();
136         for (int j=0; j<a.length; j++) {
137             i.next();
138             i.set((T)a[j]);
139         }
140     }

Collections sort calls Arrays.sort

java.util.Arrays:

1218    public static <T> void More ...sort(T[] a, Comparator<? super T> c)   {
1219        T[] aux = (T[])a.clone();
1220        if (c==null)
1221            mergeSort(aux, a, 0, a.length, 0);
1222        else
1223            mergeSort(aux, a, 0, a.length, 0, c);
1224    }

Arrays.sort calls Arrays.mergeSort and your answer is on the line 1157.

1145
1146    private static void More ...mergeSort(Object[] src,
1147                                  Object[] dest,
1148                                  int low,
1149                                  int high,
1150                                  int off) {
1151        int length = high - low;
1152
1153        // Insertion sort on smallest arrays
1154        if (length < INSERTIONSORT_THRESHOLD) {
1155            for (int i=low; i<high; i++)
1156                for (int j=i; j>low &&
1157                         ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
1158                    swap(dest, j, j-1);
1159            return;
1160        }
1161
1162        // Recursively sort halves of dest into src
1163        int destLow  = low;
1164        int destHigh = high;
1165        low  += off;
1166        high += off;
1167        int mid = (low + high) >>> 1;
1168        mergeSort(dest, src, low, mid, -off);
1169        mergeSort(dest, src, mid, high, -off);
1170
1171        // If list is already sorted, just copy from src to dest.  This is an
1172        // optimization that results in faster sorts for nearly ordered lists.
1173        if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
1174            System.arraycopy(src, low, dest, destLow, length);
1175            return;
1176        }
1177
1178        // Merge sorted halves (now in src) into dest
1179        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1180            if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
1181                dest[i] = src[p++];
1182            else
1183                dest[i] = src[q++];
1184        }
1185    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!