Both methods are in (n log(n)) time: adding an item to an ArrayList
has a worst case of (n), but usually is done in constant time, when the backing array is long enough. The actual sorting is in (n log(n)).
According to the docs, adding an item to a TreeSet
is in (log n). You do this n times, so overall time complexity is (n log(n)).
However Collections.sort
seems faster in practice.
This code snippet prints timeArray: 380
and timeTree: 714
.
import java.util.Collections;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Random;
class Test {
public static void main(String[] args) {
int steps = 1000000; // one million lines
System.out.print("timeArray: ");
System.out.println(timeArray(steps));
System.out.print("timeTree: ");
System.out.println(timeTree(steps));
}
private static long timeArray(int steps) {
long t1 = System.currentTimeMillis();
ArrayList<Integer> arr = new ArrayList<>();
Random rnd = new Random();
for (int i = 0; i < steps; i++) {
arr.add(rnd.nextInt());
}
Collections.sort(arr);
return System.currentTimeMillis() - t1;
}
private static long timeTree(int steps) {
long t1 = System.currentTimeMillis();
TreeSet<Integer> tree = new TreeSet<>();
Random rnd = new Random();
for (int i = 0; i < steps; i++) {
tree.add(rnd.nextInt());
}
return System.currentTimeMillis() - t1;
}
}