If you tend to use TreeSet , the following code may serve the purpose:
TreeSet<Integer> copied = new TreeSet<>(); for (int i = 0; i < array.length; i++) { copied.add(i); }
I tried to check the difference and actually depends on the size of the data as well as the data in the array.
However, I cannot comment on how deterministic this method is, but I am sure that I will conduct several experiments and publish my results here.
UPDATE:
I used the following code to test TreeSet performance
import java.util.Arrays; import java.util.Random; import java.util.TreeSet; class TestArrayCopy { public static void main(String[] arg) throws java.io.IOException { for (int i = 1; i <= 10; i++) { int arr[] = randomArray(); System.out.println("Array Size: " + arr.length + ". Results for case #" + i); System.out.println("Using Array Copy:"); copyAndSort(arr); System.out.println("Using Tree Set:"); useTreeSet(arr); System.out.println("----------------------------"); } } public static void copyAndSort(int array[]) { long start = System.nanoTime(); for (int j = 0; j < 100; j++) { int copied[] = Arrays.copyOf(array, array.length); Arrays.sort(copied); } long end = System.nanoTime(); System.out.println(end - start); } public static void useTreeSet(int array[]) { long start = System.nanoTime(); for (int j = 0; j < 100; j++) { TreeSet<Integer> copied = new TreeSet<>(); for (int i = 0; i < array.length; i++) { copied.add(i); } } long end = System.nanoTime(); System.out.println(end - start); } public static int[] randomArray() { Random random = new Random(); int len = 100000 + random.nextInt(1000000); int arr[] = new int[len]; for (int i = 0; i < len; i++) { arr[i] = random.nextInt(1000000); } return arr; } }
The following results were achieved on a 64-bit Core-i7 system with Java 8:
Array size: 616568. Results for case No. 1
Using the Copy array:
7692926921
Using a set of trees:
16336650396
----------------------------
Array Size: 390270. Results for Case # 2
Using the Copy array:
4441066232
Using a set of trees:
9306742954
----------------------------
Array Size: 658410. Results for Case No. 3
Using the Copy array:
8534144532
Using a set of trees:
17721764026
----------------------------
Array Size: 1021396. Results for Case # 4
Using the Copy array:
13573512678
Using a set of trees:
31341152398
----------------------------
Array Size: 1034872. Results for Case # 5
Using the Copy array:
13298690836
Using a set of trees:
30950793986
----------------------------
Array Size: 466014. Results for Case # 6
Using the Copy array:
5501196272
Using a set of trees:
11704757934
----------------------------
Array Size: 190231. Results for Case # 7
Using the Copy array:
1662270714
Using a set of trees:
4465174267
----------------------------
Array Size: 681150. Results for Case # 8
Using the Copy array:
8262756493
Using a set of trees:
19079310588
----------------------------
Array Size: 627257. Results for Case # 9
Using the Copy array:
6725984653
Using a set of trees:
14468898852
----------------------------
Array Size: 397189. Results for Case # 10
Using the Copy array:
3122214311
Using a set of trees:
7356182877
----------------------------
From these results it is clear that TreeSet is much slower than sorting a duplicate array. Good exercise for me.
Bhoot source share