The basic concepts of Fisher-Yates shuffle (Knuth shuffle ):
To shuffle an array a of n elements (indices 0 .. N-1 ):
For I from n −1 downto 1 do
J random integer with 0 ≤ j ≤ I
Exchange a [j] and a [I]
The JDK source code is as follows:
Copy codeThe Code is as follows :/**
* Moves every element of the List to a random new position in the list.
*
* @ Param list
* The List to shuffle
*
* @ Throws UnsupportedOperationException
* When replacing an element in the List is not supported
*/
Public static void shuffle (List <?> List ){
Shuffle (list, new Random ());
}
/**
* Moves every element of the List to a random new position in the list
* Using the specified random number generator.
*
* @ Param list
* The List to shuffle
* @ Param random
* The random number generator
*
* @ Throws UnsupportedOperationException
* When replacing an element in the List is not supported
*/
@ SuppressWarnings ("unchecked ")
Public static void shuffle (List <?> List, Random random ){
If (! (List instanceof RandomAccess )){
Object [] array = list. toArray ();
For (int I = array. length-1; I> 0; I --){
Int index = random. nextInt (I + 1 );
If (index <0 ){
Index =-index;
}
Object temp = array [I];
Array [I] = array [index];
Array [index] = temp;
}
Int I = 0;
ListIterator <Object> it = (ListIterator <Object>) list
. ListIterator ();
While (it. hasNext ()){
It. next ();
It. set (array [I ++]);
}
} Else {
List <Object> rawList = (List <Object>) list;
For (int I = rawList. size ()-1; I> 0; I --){
Int index = random. nextInt (I + 1 );
If (index <0 ){
Index =-index;
}
RawList. set (index, rawList. set (I, rawList. get (index )));
}
}
}
The test code uses multiple containers to ensure the same initialization in each case:Copy codeThe Code is as follows: public class implements huffle {
Public static int temp = 0;
Public static long start;
Public static long end;
Public static void main (final String args []) {
Object changeTemp;
List <Integer> numList = new ArrayList <Integer> ();
List <Integer> firstList = new ArrayList <Integer> ();
List <Integer> secondList = new ArrayList <Integer> ();
List <Integer> thirdList = new ArrayList <Integer> ();
List <Integer> fourthList = new ArrayList <Integer> ();
For (int I = 1; I <= 100000; I ++ ){
NumList. add (I );
FirstList. add (I );
SecondList. add (I );
ThirdList. add (I );
FourthList. add (I );
}
// First shuffle, use changeTemp
GetStartTime ();
Int randInt = 0;
For (int I = 0, length = firstList. size (); I <length; I ++ ){
RandInt = getRandom (I, firstList. size ());
ChangeTemp = firstList. get (I );
FirstList. set (I, firstList. get (randInt ));
FirstList. set (randInt, cmdhuffle. temp );
}
GetEndTime ("first shuffle run time ");
// Second shuffle, exchange list
GetStartTime ();
For (int I = 0, length = secondList. size (); I <length; I ++ ){
RandInt = getRandom (I, secondList. size ());
SecondList. set (I, secondList. set (randInt, secondList. get (I )));
}
GetEndTime ("second shuffle run time ");
// Third shuffle, change generate random int
GetStartTime ();
Object [] tempArray = thirdList. toArray ();
Random rand = new Random ();
Int j = 0;
For (int I = tempArray. length-1; I> 0; I --){
J = rand. nextInt (I + 1 );
ThirdList. set (I, thirdList. set (j, thirdList. get (I )));
}
GetEndTime ("third shuffle run time ");
// Fourth shuffle, simulate java shuffle
GetStartTime ();
Random random = new Random ();
If (! (FourthList instanceof RandomAccess )){
Object [] array = fourthList. toArray ();
For (int I = array. length-1; I> 0; I --){
Int index = random. nextInt (I + 1 );
If (index <0 ){
Index =-index;
}
Object temp = array [I];
Array [I] = array [index];
Array [index] = temp;
}
Int I = 0;
ListIterator <Integer> it = (ListIterator <Integer>) fourthList. listIterator ();
While (it. hasNext ()){
It. next ();
It. set (Integer) array [I ++]);
}
} Else {
List <Integer> rawList = (List <Integer>) fourthList;
For (int I = rawList. size ()-1; I> 0; I --){
Int index = random. nextInt (I + 1 );
If (index <0 ){
Index =-index;
}
RawList. set (index, rawList. set (I, rawList. get (index )));
}
}
GetEndTime ("fourth shuffle run time ");
// Java shuffle
GetStartTime ();
Collections. shuffle (numList );
GetEndTime ("java shuffle run time ");
}
Public static void swap (int a, int B ){
Export huffle. temp =;
A = B;
B = maid. temp;
}
Public static int getRandom (final int low, final int high ){
Return (int) (Math. random () * (high-low) + low );
}
Public static void getStartTime (){
Using huffle. start = System. nanoTime ();
}
Public static void getEndTime (final String s ){
Using huffle. end = System. nanoTime ();
System. out. println (s + ":" + (using huffle. end-using huffle. start) + "ns ");
}
}
If the value is small, for example, 100000, the output is probably:
First shuffle run time: 85029499ns
Second shuffle run time: 80909474ns
Third shuffle run time: 71543926ns
Fourth shuffle run time: 76520595ns
Java shuffle run time: 61027643ns
First shuffle run time: 82326239ns
Second shuffle run time: 78575611ns
Third shuffle run time: 95009632ns
Fourth shuffle run time: 105946897ns
Java shuffle run time: 90849302ns
First shuffle run time: 84539840ns
Second shuffle run time: 85965575ns
Third shuffle run time: 101814998ns
Fourth shuffle run time: 113309672ns
Java shuffle run time: 35089693ns
First shuffle run time: 87679863ns
Second shuffle run time: 79991814ns
Third shuffle run time: 73720515ns
Fourth shuffle run time: 78w.61ns
Java shuffle run time: 64146465ns
First shuffle run time: 8431420.ns
Second shuffle run time: 80074803ns
Third shuffle run time: 74001283ns
Fourth shuffle run time: 79931321ns
Java shuffle run time: 86427540ns
First shuffle run time: 84315523ns
Second shuffle run time: 814681_ns
Third shuffle run time: 75052284ns
Fourth shuffle run time: 79461407ns
Java shuffle run time: 66607729ns
The running results may be different for multiple times, but the basic java built-in shuffle is the fastest, followed by the third method. The first method takes the longest time.
For the 10000000 level, it is roughly as follows:
First shuffle run time: 2115703288ns
Second shuffle run time: 3114045871ns
Third shuffle run time: 4664109798ns
Fourth shuffle run time: 2962686695ns
Java shuffle run time: 3246883026ns first shuffle run time: 2165398466ns
Second shuffle run time: 3129558913ns
Third shuffle run time: 4147859664ns
Fourth shuffle run time: 2911849942ns
Java shuffle run time: 4311703487ns first shuffle run time: 2227462247ns
Second shuffle run time: 3279548770ns
Third shuffle run time: 4704344954ns
Fourth shuffle run time: 2934735980ns
Java shuffle run time: 3933172427ns first shuffle run time: 2200158789ns
Second shuffle run time: 3172666791ns
Third shuffle run time: 4715631517ns
Fourth shuffle run time: 2950817535ns
Java shuffle run time: 3387425676ns first shuffle run time: 2201124449ns
Second shuffle run time: 3203823874ns
Third shuffle run time: 20179926278ns
Fourth shuffle run time: 2913690411ns
Java shuffle run time: 3571313813ns first shuffle run time: 21630530000ns
Second shuffle run time: 3073889926ns
Third shuffle run time: 4493831518ns
Fourth shuffle run time: 2852713887ns
Java shuffle run time: 3773602415ns
We can see that the first method is the fastest, and the fourth method is the slowest. Java built-in shuffle speed is not ideal.
When using the java library for big data processing, you can consider other methods.