从一亿条数据中取出top100

13年前
import java.util.Random;  import java.util.TreeSet;    public class Top100  {   public static void main(String[] args)   {    long begin = System.currentTimeMillis();    int[] numarray = new int[100000000];    Random random = new Random();    for (int i = 0; i < numarray.length; ++i)    {     numarray[i] = random.nextInt(numarray.length);    }      long middle = System.currentTimeMillis();    TreeSet<Integer> top = new TreeSet<Integer>();    for (int i = 0; i < numarray.length; ++i)    {     if (top.size() < 100)     {      top.add(numarray[i]);      continue;     }       if (numarray[i] > top.first())     {      top.pollFirst();      top.add(numarray[i]);     }    }      long end = System.currentTimeMillis();    // System.out.println(top);    System.out.printf("排序用时:%d\n总共用时:%d\n", end - middle, end - begin);   }  }
运行时可能内存溢出,可使用下面命令执行:

java -Xms128M -Xmx768M Top100