In fact, this is to investigate high efficiency, solve the problem of conflict

Take 100 as an example. When 1000W is used, you can change the value. When 1-100 is convenient, the output check is correct.

Build an array of value sizes and store values in the 1-value range by subscript.

Once you have such an array, use random.nextint (value) to randomly generate values in the range [0,value] each time, swap count+value with count+value as the subscript for two numbers at count+value and position count. For example, swap list[0+5] with list[0]. Next count++, swap a position with list[1], which is equivalent to randomly selecting the 0th position and the first position of the array. Count +value cannot exceed the value of the original value.

public static void main(String[] args) {

Random random = new Random();

long start = System.currentTimeMillis();

int value = 100;

// Using arrays is faster

int[] list = new int[value];

for (int j = 0; j < value; ++j) {

list[j] = j+1;

}

int index = 0;

int count = 0;

int tmp = 0;

while (value > 0) {

index = random.nextInt(value);

//System.out.println(list[count + index]);

tmp = list[count + index];

list[count + index] = list[count];

list[count] = tmp;

++count;

–value;

}

long end = System.currentTimeMillis();

//—- Verify that the information is correct

Arrays.sort(list);

int i = 0, size = list.length;

for (; i < size; ++i) {

//System.out.println(list[i]);

if (list[i] ! = (i + 1))

System.out.println(i + “error” + list[i]);

}

//—- Verify that the information is correct

System.out.println(“creat4——“);

System.out.println(” execution time: “+ (end-start) / 1000f +” seconds “);

System.out.println(” end, set size + list.length “);

}

Actual test results:

creat3——

Execution time: 5.169 seconds

That’s it. The set size is 10000000

creat4——

Execution time: 0.729 seconds

That’s it. The set size is 10000000

If you like this article, please give it a thumbs up and leave a comment. Be sure to follow me every day and keep updating me with technical tips, interesting career stories and lots of interview information

If you like this article, please give it a thumbs up and leave a comment. Be sure to follow me every day and keep updating me with technical tips, interesting career stories and lots of interview information