jdk8下 ArrayList与LinedList 排序效率对比
Posted but999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于jdk8下 ArrayList与LinedList 排序效率对比的主要内容,如果未能解决你的问题,请参考以下文章
Java ArrayList底层实现原理源码详细分析Jdk8