The real problem here is that the OP did not tell us about the real problem. Thus, many people are aware of data structures and publish answers without even thinking.
The real symptom, as pointed out in an OP comment, is that it takes 700 ms to place the rows in the TreeSet, and another 700 ms to copy this TreeSet to an ArrayList. Obviously, the program does not do what the OP thinks, since the copy should take no more than a few microseconds. In fact, the program below, running on my ancient Thinkpad, only takes 360 ms to create 100,000 random rows, put them in a TreeSet and copy this TreeSet to an ArrayList.
However, the OP chose the answer (twice). Perhaps if / when the OP decides to think about a real problem, this SSCCE example will be useful. He is CW, so feel free to edit it.
import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.TreeSet; public class Microbench { public static void main(String[] argv) throws Exception { ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); long start = threadBean.getCurrentThreadCpuTime(); executeTest(); long finish = threadBean.getCurrentThreadCpuTime(); double elapsed = (finish - start) / 1000000.0; System.out.println(String.format("elapsed time = %7.3f ms", elapsed)); } private static List<String> executeTest() { String[] data = generateRandomStrings(100000); TreeSet<String> set = new TreeSet<String>(); for (String s : data) set.add(s); return new ArrayList<String>(set); } private static String[] generateRandomStrings(int size) { Random rnd = new Random(); String[] result = new String[size]; for (int ii = 0 ; ii < size ; ii++) result[ii] = String.valueOf(rnd.nextLong()); return result; } }
source share