Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When would you use a java.util.LinkedList [duplicate]

Tags:

java

Possible Duplicate:
When to use LinkedList<> over ArrayList<>?

This is a genuine attempt to know when would one use a LinkedList;

From what i understand since the java.util.LinkedList doesn't support random access, the only way to get the nth element is to skip from 1 to (n-1) or use get(n) which itself is very inefficient. So why would one use a LinkedList? An ArrayList would serve for most part unless you want to iterate the collection from both sides using a ListIterator?

like image 618
sachinrahulsourav Avatar asked Mar 19 '12 17:03

sachinrahulsourav


1 Answers

Think about this method:

List list = // choose your list here
list.add(0, new Object());

For large lists, LinkedList will heavily out-perform ArrayList. The same applies for

list.remove(0);

... and many other methods. For more information, I suggest reading about the java.util.Deque interface, which is also implemented by LinkedList

like image 68
Lukas Eder Avatar answered Sep 30 '22 16:09

Lukas Eder