ArrayList 还是 LinkedList 更适合排序? [英] Is an ArrayList or a LinkedList better for sorting?
问题描述
我想使用需要时不时排序的数据结构.数据结构的大小几乎不会超过1000项.
I want to use data structure that needs to be sorted every now and again. The size of the data structure will hardly exceed 1000 items.
哪个更好 - ArrayList
或 LinkedList
?
Which one is better - ArrayList
or LinkedList
?
哪种排序算法更好用?
推荐答案
在 Java 7 之前,它没有任何区别,因为 Collections.sort
会将列表的内容转储到数组中.
Up to Java 7, it made no difference because Collections.sort
would dump the content of the list into an array.
在 Java 8 中,使用 ArrayList
应该会稍微快一些,因为 Collections.sort
将调用 List.sort
和 ArrayList
有一个专门的版本,可以直接对后备数组进行排序,保存一个副本.
With Java 8, using an ArrayList
should be slightly faster because Collections.sort
will call List.sort
and ArrayList
has a specialised version that sorts the backing array directly, saving a copy.
所以最重要的是 ArrayList
更好,因为它根据 Java 版本提供相似或更好的性能.
So bottom line is ArrayList
is better as it gives a similar or better performance depending on the version of Java.
这篇关于ArrayList 还是 LinkedList 更适合排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!