Is an ArrayList or a LinkedList better for sorting?

后端 未结 5 1120
情深已故
情深已故 2020-12-10 11:13

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.

Which one is better - A

5条回答
  •  眼角桃花
    2020-12-10 11:50

    Up to Java 7, it made no difference because Collections.sort would dump the content of the list into an array.

    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.

    So bottom line is ArrayList is better as it gives a similar or better performance depending on the version of Java.

提交回复
热议问题