码迷,mamicode.com
首页 > 编程语言 > 详细

jdk8下 ArrayList与LinedList 排序效率对比

时间:2020-04-26 21:15:02      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:mil   linedlist   lis   time   comparing   get   int   list   cost   

总结: 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 排序效率对比

标签:mil   linedlist   lis   time   comparing   get   int   list   cost   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!