Home Ask Login Register

Developers Planet

Your answer is one click away!

Behrouz takhti February 2016

Java Collection (LinkedList Concepts)

When I declare LinkedList like:

List<String> names = new LinkedList<String>();

it does not support any of the LinkedList's special methods (ex: names.peekLast(), names.pollFirst() )

But when I declare like:

LinkedList<String> names = new LinkedList<String>();

then it supports these methods.

Yes, it is obvious that reason is the reference, as LinkedList contains that's methods and List does not have!

But my question is that when I want to work with LinkedList, which one is better and correct? Or what is the usage of them?


Eran February 2016

If you need to use LinkedList methods that don't exist in List, you should use a LinkedList reference (you could use a List reference and cast to LinkedList in order to call LinkedList specific methods, but that would make less sense).

Otherwise, it is preferable to use the List interface for holding the reference, since it makes your code more generic, since it won't depend on any specific List implementation.

SarthAk February 2016

Well, List is basically backed by an array which is usually bigger than the current number of items. The elements are put in an array, and a new array is created when the old one runs out of space. This is fast for access by index, but slow at removing or inserting elements within the list or at the start. Adding/removing entries at the end of the list is reasonably cheap.

LinkedList is a doubly-linked list - each node knows its previous entry and its next one. This is fast for inserting after/before a particular node (or the head/tail), but slow at access by index.

LinkedList will usually take more memory than List because it needs space for all those next/previous references - and the data will probably have less locality of reference, as each node is a separate object. On the other hand, a List can have a backing array which is much larger than its current needs.

Reference from Difference between List<T> and LinkedList<T>

You can also refer to oracle docs

Linked List

All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.


The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

Krishna February 2016

Well there is very simple explanation regarding that is List<> is like array which is making new array when its running out of space. And LinkedList<> is like doubly-linked list where each an every node will have link of previous node as well as next node.

More of that you can search from oracle docs https://docs.oracle.com/javase/7/docs/api/java/util/List.html

and https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

You can differentiate by your self. :)

Post Status

Asked in February 2016
Viewed 3,587 times
Voted 11
Answered 3 times


Leave an answer

Quote of the day: live life