中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

Java集合---Arrays類源(yuan)碼(ma)解(jie)析(xi)

一、Arrays.sort()數組(zu)排(pai)序

Java Arrays中提供(gong)了對所有類型的排序。其中主要分為Primitive(8種基本類型)和(he)Object兩大類。

  基(ji)本(ben)類(lei)型:采用(yong)調優的快(kuai)速排序;

  對象類型:采用改(gai)進的歸(gui)并(bing)排(pai)序。

 

1、對于基本類型源碼分析如下(以int[]為例):

  Java對(dui)(dui)(dui)Primitive(int,float等(deng)原型數據)數組采(cai)用(yong)快速排序(xu),對(dui)(dui)(dui)Object對(dui)(dui)(dui)象數組采(cai)用(yong)歸并排序(xu)。對(dui)(dui)(dui)這一區別,sun在(zai)<<The Java Tutorial>>中做(zuo)出(chu)的解(jie)釋(shi)如下(xia):

  The sort operation uses a slightly optimized merge sort algorithm that is fast and stable:

  * Fast: It is guaranteed to run in n log(n) time and runs substantially faster on nearly sorted lists. Empirical tests showed it to be as fast as a highly optimized quicksort. A quicksort is generally considered to be faster than a merge&nbsp;sort but isn't stable and doesn't guarantee n log(n) performance.

  * Stable: It doesn't reorder equal elements. This is important if you sort the same list repeatedly on different attributes. If a user of a mail program sorts the inbox by mailing date and then sorts it by sender, the user naturally expects that the now-contiguous list of messages from a given sender will (still) be&nbsp;sorted by mailing date. This is guaranteed only if the second sort was stable.

  也就是說,優化的(de)歸并排序既(ji)快速(nlog(n))又穩定。

  對(dui)(dui)于對(dui)(dui)象的排序,穩定性很重要。比(bi)如成(cheng)績單,一開始可能(neng)是(shi)按人員的學(xue)號順序排好了(le)的,現(xian)在讓我們用成(cheng)績排,那么你應該保證,本來張三在李四前面,即使他們成(cheng)績相同(tong),張三不(bu)能(neng)跑到李四的后(hou)面去。

  而快速排序是不(bu)穩(wen)定的(de)(de),而且(qie)最(zui)壞情況下的(de)(de)時(shi)間復雜度是O(n^2)。

  另(ling)外,對象數(shu)組中保存的只是對象的引用(yong),這樣多次(ci)移位并(bing)不會造成額外的開銷,但是,對象數(shu)組對比(bi)(bi)(bi)較(jiao)次(ci)數(shu)一般比(bi)(bi)(bi)較(jiao)敏(min)感,有可能對象的比(bi)(bi)(bi)較(jiao)比(bi)(bi)(bi)單純(chun)數(shu)的比(bi)(bi)(bi)較(jiao)開銷大很多。歸(gui)并(bing)排序(xu)(xu)在這方面比(bi)(bi)(bi)快速排序(xu)(xu)做得更好,這也是選擇它作為(wei)對象排序(xu)(xu)的一個重要原因之一。

  排序(xu)優化:實現中快排和歸(gui)(gui)并都(dou)采用遞歸(gui)(gui)方式,而(er)在遞歸(gui)(gui)的底(di)層,也(ye)就是(shi)待排序(xu)的數(shu)組長度小于7時,直接使用冒泡排序(xu),而(er)不再(zai)遞歸(gui)(gui)下去。

  分析:長(chang)度為6的(de)(de)數組(zu)冒(mao)泡排(pai)序(xu)(xu)總比(bi)較次(ci)數最多也(ye)就1+2+3+4+5+6=21次(ci),最好(hao)情況下只有6次(ci)比(bi)較。而快(kuai)排(pai)或歸并涉及到(dao)遞歸調用等的(de)(de)開銷(xiao),其時(shi)間效率在n較小時(shi)劣勢就凸(tu)顯了(le),因此這里(li)采用了(le)冒(mao)泡排(pai)序(xu)(xu),這也(ye)是對(dui)快(kuai)速排(pai)序(xu)(xu)極(ji)重要(yao)的(de)(de)優化。

 

  源碼中的快速排(pai)序,主要做了以下幾個方面的優(you)化:

  1)當待排(pai)序(xu)(xu)的(de)數(shu)組中的(de)元(yuan)素個(ge)數(shu)較(jiao)少時(shi),源碼中的(de)閥值為7,采用的(de)是插入(ru)排(pai)序(xu)(xu)。盡管插入(ru)排(pai)序(xu)(xu)的(de)時(shi)間(jian)復(fu)雜度為0(n^2),但(dan)是當數(shu)組元(yuan)素較(jiao)少時(shi),插入(ru)排(pai)序(xu)(xu)優于快速(su)排(pai)序(xu)(xu),因為這(zhe)時(shi)快速(su)排(pai)序(xu)(xu)的(de)遞歸(gui)操作(zuo)影響性能。

  2)較(jiao)好的(de)選擇(ze)(ze)了劃分(fen)元(yuan)(基(ji)準(zhun)元(yuan)素)。能夠將(jiang)數組(zu)(zu)分(fen)成大致兩個相等的(de)部分(fen),避免出(chu)現最壞的(de)情況。例如當數組(zu)(zu)有序的(de)的(de)情況下(xia),選擇(ze)(ze)第一個元(yuan)素作為劃分(fen)元(yuan),將(jiang)使得算法的(de)時間(jian)復(fu)雜度達到O(n^2).

  源碼中(zhong)選擇劃分元的方法:

    當數組(zu)大小為 size=7 時&nbsp;,取數組(zu)中間元(yuan)素作為劃分元(yuan)。int n=m>>1;(此方法值得借鑒)

    當數組(zu)大(da)小 7<size<=40時,取(qu)首、中、末(mo)三(san)個元素中間大(da)小的元素作(zuo)為劃分元。

    當(dang)數組大小(xiao) size>40 時 ,從待(dai)排數組中(zhong)較(jiao)均勻的選擇9個(ge)(ge)元(yuan)(yuan)素(su),選出一(yi)個(ge)(ge)偽中(zhong)數做為劃分元(yuan)(yuan)。

  3)根據劃分元 v ,形成(cheng)不變式 v* (<v)* (>;v)* v*

  普(pu)通的快速排序算(suan)法,經過一(yi)次劃分(fen)后,將(jiang)劃分(fen)元(yuan)排到(dao)素組較中間的位置,左邊的元(yuan)素小于(yu)劃分(fen)元(yuan),右(you)邊的元(yuan)素大于(yu)劃分(fen)元(yuan),而沒有(you)將(jiang)與(yu)劃分(fen)元(yuan)相等的元(yuan)素放在其附近,這一(yi)點,在Arrays.sort()中得到(dao)了較大的優化。

  舉例:15、93、15、41、6、15、22、7、15、20

  因  7<size<=40,所以在15、6、和20 中選擇v&nbsp;= 15 作為劃分(fen)元。

  經過(guo)一次換分后(hou): 15、15、7、6、41、20、22、93、15、15. 與劃分元相等的(de)(de)元素都(dou)移到(dao)了(le)素組的(de)(de)兩邊。

  接下(xia)來將與劃分元(yuan)相等(deng)的元(yuan)素移到數組中間來,形(xing)成(cheng):7、6、15、15、15、15、41、20、22、93.

  最后遞歸對兩(liang)個區間(jian)進行排序[7、6]和[41、20、22、93].

 

  部分源代碼(ma)(一)如(ru)下:

1 package com.util;
  2 
  3 public class ArraysPrimitive {
  4     private ArraysPrimitive() {}
  5 
  6     /**
  7     * 對指(zhi)定的 int 型數(shu)組按數(shu)字(zi)升序(xu)(xu)進(jin)行排序(xu)(xu)。
  8      */
  9     public static void sort(int[] a) {
 10         sort1(a, 0, a.length);
 11    }
 12     
 13     /**
 14     * 對(dui)指定 int 型數組的指定范圍按數字升序(xu)進行排序(xu)。
 15      */
 16     public static void sort(int[] a, int fromIndex, int toIndex) {
 17        rangeCheck(a.length, fromIndex, toIndex);
 18         sort1(a, fromIndex, toIndex - fromIndex);
 19    }
 20 
 21     private static void sort1(int x[], int off, int len) {
 22         /*
 23         * 當待排(pai)序(xu)(xu)的數(shu)組中的元(yuan)素個數(shu)小(xiao)于 7 時,采用插(cha)入(ru)排(pai)序(xu)(xu) 。
 24         * 
 25         * 盡(jin)管插(cha)入(ru)排(pai)序(xu)(xu)的時間復(fu)雜(za)度為O(n^2),但是當數(shu)組元(yuan)素較(jiao)少時, 插(cha)入(ru)排(pai)序(xu)(xu)優(you)于快速(su)(su)排(pai)序(xu)(xu),因為這時快速(su)(su)排(pai)序(xu)(xu)的遞歸操作影響性(xing)能。
 26          */
 27         if (len < 7) {
 28             for (int i = off; i < len + off; i++)
 29                 for (int j = i; j > off && x[j - 1] > x[j]; j--)
 30                     swap(x, j, j - 1);
 31             return;
 32        }
 33         /*
 34         * 當待排序(xu)的數(shu)組(zu)中的元(yuan)素(su)個(ge)數(shu)大(da)于(yu) 或等(deng)于(yu)7 時,采用快速排序(xu) 。
 35         * 
 36         * Choose a partition element, v
 37         * 選取一個(ge)劃分(fen)元(yuan),V
 38         * 
 39         * 較好的選擇了劃分(fen)元(yuan)(基準元(yuan)素(su))。能夠(gou)將(jiang)數(shu)組(zu)分(fen)成大(da)致兩(liang)個(ge)相等(deng)的部分(fen),避免出(chu)現(xian)最壞(huai)的情(qing)況。例如當數(shu)組(zu)有序(xu)的的情(qing)況下,
 40         * 選擇第一個(ge)元(yuan)素(su)作為(wei)劃分(fen)元(yuan),將(jiang)使得算法的時間復雜度達到O(n^2).
 41          */
 42         // 當數組(zu)大小(xiao)為size=7時(shi) ,取數組(zu)中間元素作為劃(hua)分元。
 43         int m = off + (len >> 1);
 44         // 當數組(zu)大(da)小 7<size<=40時,取首、中、末 三個元(yuan)素中間(jian)大(da)小的元(yuan)素作為劃(hua)分元(yuan)。
 45         if (len > 7) {
 46             int l = off;
 47             int n = off + len - 1;
 48             /*
 49             * 當數(shu)組(zu)大小  size>40 時 ,從待排數(shu)組(zu)中(zhong)較均(jun)勻的選擇9個元(yuan)素,
 50             * 選出一(yi)個偽中(zhong)數(shu)做為劃(hua)分元(yuan)。
 51              */
 52             if (len > 40) {
 53                 int s = len / 8;
 54                 l = med3(x, l, l + s, l + 2 * s);
 55                 m = med3(x, m - s, m, m + s);
 56                 n = med3(x, n - 2 * s, n - s, n);
 57            }
 58             // 取出中(zhong)間(jian)大小的(de)元素(su)的(de)位(wei)置。
 59             m = med3(x, l, m, n); // Mid-size, med of 3
 60        }
 61         
 62         //得到(dao)劃分元V
 63         int v = x[m];
 64         
 65         // Establish Invariant: v* (<v)* (>v)* v*
 66         int a = off, b = a, c = off + len - 1, d = c;
 67         while (true) {
 68             while (b <= c && x[b] <= v) {
 69                 if (x[b] == v)
 70                     swap(x, a++, b);
 71                 b++;
 72            }
 73             while (c >= b && x[c] >= v) {
 74                 if (x[c] == v)
 75                     swap(x, c, d--);
 76                 c--;
 77            }
 78             if (b > c)
 79                 break;
 80             swap(x, b++, c--);
 81        }
 82         // Swap partition elements back to middle
 83         int s, n = off + len;
 84         s = Math.min(a - off, b - a);
 85         vecswap(x, off, b - s, s);
 86         s = Math.min(d - c, n - d - 1);
 87         vecswap(x, b, n - s, s);
 88         // Recursively sort non-partition-elements
 89         if ((s = b - a) > 1)
 90            sort1(x, off, s);
 91         if ((s = d - c) > 1)
 92             sort1(x, n - s, s);
 93    }
 94     
 95     /**
 96     * Swaps x[a] with x[b].
 97      */
 98     private static void swap(int x[], int a, int b) {
 99         int t = x[a];
100         x[a] = x[b];
101         x[b] = t;
102    }
103     
104     /**
105     * Swaps x[a .. (a+n-1)] with x[b .. (b+n-1)].
106      */
107     private static void vecswap(int x[], int a, int b, int n) {
108     for (int i=0; i<n; i++, a++, b++)
109        swap(x, a, b);
110    }
111     
112     /**
113     * Returns the index of the median of the three indexed integers.
114      */
115     private static int med3(int x[], int a, int b, int c) {
116         return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
117                 : (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
118    }
119 
120     /**
121     * Check that fromIndex and toIndex are in range, and throw an
122     * appropriate exception if they aren't.
123      */
124     private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
125         if (fromIndex > toIndex)
126             throw new IllegalArgumentException("fromIndex(" + fromIndex
127                     + ") > toIndex(" + toIndex + ")");
128         if (fromIndex < 0)
129             throw new ArrayIndexOutOfBoundsException(fromIndex);
130         if (toIndex > arrayLen)
131             throw new ArrayIndexOutOfBoundsException(toIndex);
132    }
133 }

測試代碼(ma)如下:

1 package com.test;
 2 
 3 import com.util.ArraysPrimitive;
 4 
 5 public class ArraysTest {
 6     public static void main(String[] args) {
 7         int [] a={15,93,15,41,6,15,22,7,15,20};
 8        ArraysPrimitive.sort(a);
 9         for(int i=0;i<a.length;i++){
10             System.out.print(a[i]+",");
11        }
12         //結(jie)果:6,7,15,15,15,15,20,22,41,93,
13    }
14 }

2、對于Object類型源碼分析(xi)如下:

  部分源代碼(二)如下:

1 package com.util;
  2 
  3 import java.lang.reflect.Array;
  4 
  5 public class ArraysObject {
  6     private static final int INSERTIONSORT_THRESHOLD = 7;
  7 
  8     private ArraysObject() {}
  9 
 10     public static void sort(Object[] a) {
 11         //java.lang.Object.clone(),理解(jie)深表復制和(he)淺(qian)表復制
 12         Object[] aux = (Object[]) a.clone();
 13         mergeSort(aux, a, 0, a.length, 0);
 14    }
 15 
 16     public static void sort(Object[] a, int fromIndex, int toIndex) {
 17        rangeCheck(a.length, fromIndex, toIndex);
 18         Object[] aux = copyOfRange(a, fromIndex, toIndex);
 19         mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
 20    }
 21 
 22     /**
 23     * Src is the source array that starts at index 0 
 24     * Dest is the (possibly larger) array destination with a possible offset 
 25     * low is the index in dest to start sorting 
 26     * high is the end index in dest to end sorting 
 27     * off is the offset to generate corresponding low, high in src
 28      */
 29     private static void mergeSort(Object[] src, Object[] dest, int low,
 30             int high, int off) {
 31         int length = high - low;
 32 
 33         // Insertion sort on smallest arrays
 34         if (length < INSERTIONSORT_THRESHOLD) {
 35             for (int i = low; i < high; i++)
 36                 for (int j = i; j > low && 
 37                         ((Comparable) dest[j - 1]).compareTo(dest[j]) > 0; j--)
 38                     swap(dest, j, j - 1);
 39             return;
 40        }
 41 
 42         // Recursively sort halves of dest into src
 43         int destLow = low;
 44         int destHigh = high;
 45         low += off;
 46         high += off;
 47         /*
 48         *  >>>:無符號右(you)移運算符
 49         *  expression1 &gt;>> expresion2:expression1的各個位向右(you)移expression2
 50         *  指(zhi)定的位數。右(you)移后左邊空出(chu)的位數用0來(lai)填充。移出(chu)右(you)邊的位被丟棄。
 51         *  例如:-14>>>2;  結果為(wei):1073741820
 52          */
 53         int mid = (low + high) >>> 1;
 54         mergeSort(dest, src, low, mid, -off);
 55         mergeSort(dest, src, mid, high, -off);
 56 
 57         // If list is already sorted, just copy from src to dest. This is an
 58         // optimization that results in faster sorts for nearly ordered lists.
 59         if (((Comparable) src[mid - 1]).compareTo(src[mid]) <= 0) {
 60            System.arraycopy(src, low, dest, destLow, length);
 61             return;
 62        }
 63 
 64         // Merge sorted halves (now in src) into dest
 65         for (int i = destLow, p = low, q = mid; i < destHigh; i++) {
 66             if (q >= high || p < mid
 67                     && ((Comparable) src[p]).compareTo(src[q]) <= 0)
 68                 dest[i] = src[p++];
 69             else
 70                 dest[i] = src[q++];
 71        }
 72    }
 73 
 74     /**
 75     * Check that fromIndex and toIndex are in range, and throw an appropriate
 76     * exception if they aren't.
 77      */
 78     private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
 79         if (fromIndex > toIndex)
 80             throw new IllegalArgumentException("fromIndex(" + fromIndex
 81                     + ") > toIndex(" + toIndex + ")");
 82         if (fromIndex < 0)
 83             throw new ArrayIndexOutOfBoundsException(fromIndex);
 84         if (toIndex > arrayLen)
 85             throw new ArrayIndexOutOfBoundsException(toIndex);
 86    }
 87 
 88     public static <T> T[] copyOfRange(T[] original, int from, int to) {
 89         return copyOfRange(original, from, to, (Class<T[]>) original.getClass());
 90    }
 91 
 92     public static <T, U> T[] copyOfRange(U[] original, int from, int to,
 93             Class<? extends T[]> newType) {
 94         int newLength = to - from;
 95         if (newLength < 0)
 96             throw new IllegalArgumentException(from + " > " + to);
 97         T[] copy = ((Object) newType == (Object) Object[].class)
 98                 ? (T[]) new Object[newLength]
 99                : (T[]) Array.newInstance(newType.getComponentType(), newLength);
100         System.arraycopy(original, from, copy, 0,
101                 Math.min(original.length - from, newLength));
102         return copy;
103    }
104 
105     /**
106     * Swaps x[a] with x[b].
107      */
108     private static void swap(Object[] x, int a, int b) {
109         Object t = x[a];
110         x[a] = x[b];
111         x[b] = t;
112    }
113 }

測(ce)試代碼(ma)如下(xia):

1 package com.test;
 2 
 3 import com.util.ArraysObject;
 4 
 5 public class ArraysObjectSortTest {
 6     public static void main(String[] args) {
 7         Student stu1=new Student(1001,100.0F);
 8         Student stu2=new Student(1002,90.0F);
 9         Student stu3=new Student(1003,90.0F);
10         Student stu4=new Student(1004,95.0F);
11         Student[] stus={stu1,stu2,stu3,stu4};
12         //Arrays.sort(stus);
13        ArraysObject.sort(stus);
14         for(int i=0;i<stus.length;i++){
15             System.out.println(stus[i].getId()+" : "+stus[i].getScore());
16        }
17         /* 1002 : 90.0
18         * 1003 : 90.0
19         * 1004 : 95.0
20         * 1001 : 100.0
21          */
22    }
23 }
24 class Student implements Comparable<Student>{
25     private int id;  //學號
26     private float score;  //成(cheng)績
27     public Student(){}
28     public Student(int id,float score){
29         this.id=id;
30         this.score=score;
31    }
32    @Override
33     public int compareTo(Student s) {
34         return (int)(this.score-s.getScore());
35    }
36     public int getId() {
37         return id;
38    }
39     public void setId(int id) {
40         this.id = id;
41    }
42     public float getScore() {
43         return score;
44    }
45     public void setScore(float score) {
46         this.score = score;
47    }
48 }

輔助理解代碼(ma):

1 package com.lang;
 2 
 3 public final class System {
 4     //System 類(lei)不能被實例(li)化(hua)。 
 5     private System() {}
 6     //在 System 類提供(gong)的設施中(zhong),有標(biao)準輸入、標(biao)準輸出和錯誤輸出流;對外部定義(yi)的屬性
 7     //和環境變量(liang)的(de)(de)訪問;加載文件和庫的(de)(de)方法;還有快(kuai)速復(fu)制數組(zu)的(de)(de)一(yi)部分的(de)(de)實用方法。
 8     /**
 9     * src and dest都必須是同類(lei)(lei)型或者可以進行轉換類(lei)(lei)型的(de)數組.
10     * @param      src      the source array.
11     * @param      srcPos   starting position in the source array.
12     * @param      dest     the destination array.
13     * @param      destPos  starting position in the destination data.
14     * @param      length   the number of array elements to be copied.
15      */
16     public static native void arraycopy(Object src, int srcPos, Object dest,
17             int destPos, int length);
18 }


1 package com.lang.reflect;
 2 
 3 public final class Array {
 4     private Array() {}
 5     
 6     //創建一個具有指定的組(zu)件類型和維度的新數組(zu)。
 7     public static Object newInstance(Class<?> componentType, int length)
 8             throws NegativeArraySizeException {
 9         return newArray(componentType, length);
10    }
11 
12     private static native Object newArray(Class componentType, int length)
13             throws NegativeArraySizeException;
14 }

二(er)、Arrays.asList

慎用ArrayList的contains方法,使(shi)用HashSet的contains方法代替(ti)

在(zai)啟動一(yi)個應用的(de)(de)時候,發現其(qi)(qi)中有(you)一(yi)處(chu)數據(ju)加(jia)載要數分鐘(zhong),剛開始(shi)以(yi)為是(shi)(shi)需要load的(de)(de)數據(ju)比較多(duo)(duo)的(de)(de)緣故,查了一(yi)下數據(ju)庫有(you)6條(tiao)左右,但(dan)是(shi)(shi)單獨寫(xie)了一(yi)個數據(ju)讀(du)取(qu)的(de)(de)方法,將這(zhe)(zhe)6萬(wan)多(duo)(duo)條(tiao)全(quan)部讀(du)過(guo)來(lai)(lai),卻只需要不(bu)到10秒(miao)鐘(zhong),就(jiu)覺得這(zhe)(zhe)里面肯定有(you)問題,于(yu)是(shi)(shi)仔細看其(qi)(qi)中的(de)(de)邏輯(ji),其(qi)(qi)中有(you)一(yi)段數據(ju)去重的(de)(de)邏輯(ji),就(jiu)是(shi)(shi)記(ji)錄中存在(zai)某幾個字段相同的(de)(de),就(jiu)認為是(shi)(shi)重復(fu)數據(ju),就(jiu)需要將重復(fu)數據(ju)給過(guo)濾掉。這(zhe)(zhe)里就(jiu)用到了一(yi)個List來(lai)(lai)存放這(zhe)(zhe)幾個字段所組成的(de)(de)主鍵,如果發現相同的(de)(de)就(jiu)不(bu)處(chu)理,代碼無(wu)非就(jiu)是(shi)(shi)下面這(zhe)(zhe)樣(yang):

 

1 List<string> uniqueKeyList = new ArrayList<string>();  
2 //......  
3 if (uniqueKeyList.contains(uniqueKey)) {  
4                    continue;  
  }

 

根據(ju)鍵去查(cha)找是(shi)(shi)不是(shi)(shi)已經存在了(le),來(lai)判斷是(shi)(shi)否重復(fu)數據(ju)。經過(guo)分析,這一塊耗費(fei)了(le)非常多的(de)時候,于是(shi)(shi)就去查(cha)看ArrayList的(de)contains方(fang)法的(de)源碼(ma),發現其最終會(hui)調用他本(ben)身的(de)indexOf方(fang)法:

 

7public int indexOf(Object elem) {  
8    if (elem == null) {  
9        for (int i = 0; i < size; i++)  
10        if (elementData[i]==null)  
11            return i;  
12    } else {  
13        for (int i = 0; i < size; i++)  
14        if (elem.equals(elementData[i]))  
15            return i;  
16    }  
17    return -1;  
18    }  

 

原來在這里他(ta)做(zuo)的(de)是遍(bian)歷整(zheng)個list進行查找(zhao),最多可能對一個鍵的(de)查找(zhao)會(hui)達到6萬(wan)多次(ci),也就是會(hui)掃描整(zheng)個List,驗怪會(hui)這么慢(man)了。

于是將原來的List替換(huan)為Set:

 

Set<string> uniqueKeySet = new HashSet<string>();  
//......  
if (uniqueKeySet.contains(uniqueKey)) {  
                    continue;  
} 

 

速(su)度一(yi)下就(jiu)上去了(le),在去重這一(yi)塊最多(duo)花費了(le)一(yi)秒(miao)鐘,為什么HashSet的(de)速(su)度一(yi)下就(jiu)上去了(le),那是因為其內部(bu)使用的(de)是Hashtable,這是HashSet的(de)contains的(de)源碼(ma):

 

public boolean contains(Object o) {  
   return map.containsKey(o);  
} 

 

關于UnsupportedOperationException異常

     在使用Arrays.asList()后調(diao)用add,remove這些(xie)method時出現java.lang.UnsupportedOperationException異常。這是由于Arrays.asList() 返回java.util.Arrays$ArrayList, 而不是ArrayList。Arrays$ArrayList和ArrayList都(dou)是繼(ji)承AbstractList,remove,add等method在AbstractList中是默認throw UnsupportedOperationException而且不作任何操(cao)作。ArrayList override這些(xie)method來對list進行(xing)操(cao)作,但(dan)是Arrays$ArrayList沒(mei)有override remove(),add()等,所以throw UnsupportedOperationException。

 

posted @ 2014-09-01 10:38  ^_TONY_^  閱讀(5864)  評論(0)    收藏  舉報