Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Is an ArrayList or a LinkedList better for sorting?

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 - ArrayList or LinkedList?

Which sorting algorithm is better to use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...