publicstaticvoidshuffle(List<?>list,Randomrnd){intsize=list.size();//SHUFFLE_THRESHOLD = 5if(size<SHUFFLE_THRESHOLD||listinstanceofRandomAccess){for(inti=size;i>1;i--)swap(list,i-1,rnd.nextInt(i));}else{Objectarr[]=list.toArray();// Shuffle arrayfor(inti=size;i>1;i--)swap(arr,i-1,rnd.nextInt(i));// Dump array back into listListIteratorit=list.listIterator();for(inti=0;i<arr.length;i++){it.next();it.set(arr[i]);}}}
publicstatic<T>voidcopy(List<?superT>dest,List<?extendsT>src){intsrcSize=src.size();if(srcSize>dest.size())thrownewIndexOutOfBoundsException("Source does not fit in dest");//COPY_THRESHOLD = 10if(srcSize<COPY_THRESHOLD||(srcinstanceofRandomAccess&&destinstanceofRandomAccess)){for(inti=0;i<srcSize;i++)dest.set(i,src.get(i));}else{ListIterator<?superT>di=dest.listIterator();ListIterator<?extendsT>si=src.listIterator();for(inti=0;i<srcSize;i++){di.next();di.set(si.next());}}}
Collection min max
List rotate
旋转,可以左旋或者右旋,第二参数为正数表示右旋,负数表示左旋.
如list = [t, a, n, k, s],则Collections.rotate(list, 1)之后变为[s, t, a, n, k].
Collections.rotate(list, 1)等效于Collections.rotate(list, -4)
//注意SynchronizedCollection定义了mutextaticclassSynchronizedCollection<E>implementsCollection<E>,Serializable{finalCollection<E>c;// Backing CollectionfinalObjectmutex;// Object on which to synchronize//...}staticclassSynchronizedList<E>extendsSynchronizedCollection<E>implementsList<E>{finalList<E>list;publicEget(intindex){synchronized(mutex){returnlist.get(index);}}publicEset(intindex,Eelement){synchronized(mutex){returnlist.set(index,element);}}//...}
staticclassCheckedCollection<E>implementsCollection<E>,Serializable{finalCollection<E>c;finalClass<E>type;//支持的类型//类型检查voidtypeCheck(Objecto){if(!type.isInstance(o))thrownewClassCastException("Attempt to insert "+o.getClass()+" element into collection with element type "+type);}}staticclassCheckedList<E>extendsCheckedCollection<E>implementsList<E>{finalList<E>list;publicEset(intindex,Eelement){typeCheck(element);//类型检查returnlist.set(index,element);}publicvoidadd(intindex,Eelement){typeCheck(element);list.add(index,element);}//...}
publicvoidensureCapacity(intminCapacity){modCount++;intoldCapacity=elementData.length;if(minCapacity>oldCapacity){ObjectoldData[]=elementData;intnewCapacity=(oldCapacity*3)/2+1;if(newCapacity<minCapacity)newCapacity=minCapacity;// minCapacity is usually close to size, so this is a win:elementData=Arrays.copyOf(elementData,newCapacity);}}
privatestaticvoidsort2(doublea[],intfromIndex,inttoIndex){finallongNEG_ZERO_BITS=Double.doubleToLongBits(-0.0d);/* * 排序过程分为三个阶段,目的是为了在排序的主流乘避免出现NaN和-0.0 *//* * 阶段1 预处理过程 : 将NaN移到数组尾部,计算-0.0的数目,并将-0.0变为0.0 */intnumNegZeros=0;//-0.0的数目inti=fromIndex,n=toIndex;while(i<n){if(a[i]!=a[i]){//a[i] != a[i]表示NaNdoubleswap=a[i];a[i]=a[--n];a[n]=swap;}else{//表示-0.0if(a[i]==0&&Double.doubleToLongBits(a[i])==NEG_ZERO_BITS){a[i]=0.0d;numNegZeros++;}i++;}}// 阶段2 : 排序主流程: quicksortsort1(a,fromIndex,n-fromIndex);// 阶段3: 将原始的-0.0变回去(numNegZeros个)if(numNegZeros!=0){intj=binarySearch0(a,fromIndex,n,0.0d);// posn of ANY zerodo{j--;}while(j>=0&&a[j]==0.0d);// j is now one less than the index of the FIRST zerofor(intk=0;k<numNegZeros;k++)a[++j]=-0.0d;}}
privatestaticintbinarySearch0(double[]a,intfromIndex,inttoIndex,doublekey){intlow=fromIndex;inthigh=toIndex-1;while(low<=high){intmid=(low+high)>>>1;doublemidVal=a[mid];intcmp;if(midVal<key){cmp=-1;// Neither val is NaN, thisVal is smaller}elseif(midVal>key){cmp=1;// Neither val is NaN, thisVal is larger}else{longmidBits=Double.doubleToLongBits(midVal);longkeyBits=Double.doubleToLongBits(key);cmp=(midBits==keyBits?0:// Values are equal(midBits<keyBits?-1:// (-0.0, 0.0) or (!NaN, NaN)1));// (0.0, -0.0) or (NaN, !NaN)}if(cmp<0)low=mid+1;elseif(cmp>0)high=mid-1;elsereturnmid;// key found}return-(low+1);// key not found.}