站长软件下载 东莞佳利热转印机 空白卫衣批发 个性服饰定制

Java Comparator 解析

今日修一排序的bug,发现其中是实现了比较器Comparator,之前也是一直在用,但是只是知其皮毛,今天便深究一下,翻其源码。

首先,大家都知道对于集合进行排序的时候,可以实现Comparator,则可以按我们的需求进行所需的排序。

主要的排序逻辑则是由compare来实现。

当返回-1时,表明不需要对传入参数的位置进行调换;

返回0时,表明值相等,也不需要进行调换。

返回1时,表明需要对值进行调换。

遂写一Demo进行了实验:

Java代码:  
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Comparator;  
  4. import java.util.List;  
  5.   
  6. public class ComparatorTest {  
  7.   
  8.     public static void main(String[] args) {  
  9.         List<Integer> list = new ArrayList<Integer>();  
  10.         list.add(1);  
  11.         list.add(4);  
  12.         list.add(3);  
  13.         list.add(6);  
  14.         System.out.println(list);  
  15.         Collections.sort(list, comparator_int);  
  16.         System.out.println(list);  
  17.     }  
  18.   
  19.     public static Comparator<Integer> comparator_int = new Comparator<Integer>() {  
  20.   
  21.         @Override  
  22.         public int compare(Integer o1, Integer o2) {  
  23.             System.out.println("o2:" + o2 + "-" + "o1:" + o1 + "=" + (o2 - o1));  
  24.             return o2 - o1;  
  25.         }  
  26.     };  
  27. }  

 结果如下:

[1, 4, 3, 6]

o2:4-o1:1=3

o2:3-o1:1=2

o2:3-o1:4=-1

o2:6-o1:1=5

o2:6-o1:3=3

o2:6-o1:4=2

[6, 4, 3, 1]

如此看,排序的内部算法似乎是冒泡一样,便跟入进去查看一番!

Collections.sort() 用的也是Arrays.sort();

Java代码:  
  1. public static <T> void sort(List<T> list, Comparator<? super T> c) {  
  2.     Object[] a = list.toArray();  
  3.     Arrays.sort(a, (Comparator)c);  
  4.     ListIterator i = list.listIterator();  
  5.     for (int j=0; j<a.length; j++) {  
  6.         i.next();  
  7.         i.set(a[j]);  
  8.     }  
  9.     }  

 继续跟下去:

Arrays.sort();

Java代码:  
  1. public static <T> void sort(T[] a, Comparator<? super T> c) {  
  2.     T[] aux = (T[])a.clone();  
  3.         if (c==null)  
  4.             mergeSort(aux, a, 0, a.length, 0);  
  5.         else  
  6.             mergeSort(aux, a, 0, a.length, 0, c);  
  7.     }  

 看到正主了:mergeSort();

Java代码: 
  1. private static void mergeSort(Object[] src,  
  2.                   Object[] dest,  
  3.                   int low, int high, int off,  
  4.                   Comparator c) {  
  5.     int length = high - low;  
  6.   
  7.     // Insertion sort on smallest arrays  
  8.     if (length < INSERTIONSORT_THRESHOLD) {  
  9.         for (int i=low; i<high; i++)  
  10.         for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)  
  11.             swap(dest, j, j-1);  
  12.         return;  
  13.     }   
  14.         // Recursively sort halves of dest into src  
  15.         int destLow  = low;  
  16.         int destHigh = high;  
  17.         low  += off;  
  18.         high += off;  
  19.         int mid = (low + high) >>> 1;  
  20.         mergeSort(dest, src, low, mid, -off, c);  
  21.         mergeSort(dest, src, mid, high, -off, c);  
  22.   
  23.         // If list is already sorted, just copy from src to dest.  This is an  
  24.         // optimization that results in faster sorts for nearly ordered lists.  
  25.         if (c.compare(src[mid-1], src[mid]) <= 0) {  
  26.            System.arraycopy(src, low, dest, destLow, length);  
  27.            return;  
  28.         }  
  29.   
  30.         // Merge sorted halves (now in src) into dest  
  31.         for(int i = destLow, p = low, q = mid; i < destHigh; i++) {  
  32.             if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)  
  33.                 dest[i] = src[p++];  
  34.             else  
  35.                 dest[i] = src[q++];  
  36.        }  
  37.     }  

 可以看到时用了二分插入排序算法。

那么之前的估测冒泡是错的,看来还是要跟进源码去看才知道呀!

原文参考自站长网:http://www.software8.co/wzjs/java/3496.html

posted on 2013-03-22 07:37  pc蛋蛋  阅读(1023)  评论(0编辑  收藏  举报