jdk8下 ArrayList与LinedList 排序效率对比

时间:2020-04-26
本文章向大家介绍jdk8下 ArrayList与LinedList 排序效率对比,主要包括jdk8下 ArrayList与LinedList 排序效率对比使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
总结: linkedlist排序性能更好,并且较arraylist更节省空间。


public static void main(String[] args) {
long startTime1 = System.currentTimeMillis();
List<xx> arrayList = new ArrayList<>(1000);
Random r = new Random();
for (int i = 0; i < 10000; i++) {
  xx a = new xx();
 a.setStartTime(r.nextLong());
arrayList.add(a);
}
long startTime2 = System.currentTimeMillis();
arrayList.sort(Comparator.comparing(xx::getStartTime));
System.out.println("handle total " + (System.currentTimeMillis() - startTime1) + " array sort cost " + (System.currentTimeMillis() - startTime2));

long startTime3 = System.currentTimeMillis();
List<xx> linkedList = new LinkedList<>();
for (int i = 0; i < 10000; i++) {
xx a = new xx();
a.setStartTime(r.nextLong());
linkedList.add(a);
}
long startTime4 = System.currentTimeMillis();
linkedList.sort(Comparator.comparing(xx::getStartTime));
System.out.println("handle total " + (System.currentTimeMillis() - startTime3) + " linked sort cost " + (System.currentTimeMillis() - startTime4));

}

handle total 122 array sort cost 115
handle total 34 linked sort cost 29

原文地址:https://www.cnblogs.com/but999/p/12781892.html