3

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?

Olivier Grégoire
  • 31,286
  • 22
  • 93
  • 132

3 Answers3

2

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.

Eran
  • 374,785
  • 51
  • 663
  • 734
0

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.

List

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.

Community
  • 1
  • 1
SarthAk
  • 1,438
  • 3
  • 15
  • 24
  • 1
    Minor nitpick: Lists are *usually* backed by arrays, but since `List` is an interface, that's not a guarantee. You could create an implementing class which uses trees to store data instead of arrays. Heck, if you really wanted to torture yourself, you could make the backing data structure a String and then use some horrible reflection code + regex to store objects. Implementation shouldn't matter if you're programming to the interface. – Mage Xy Feb 08 '16 at 16:05
-1

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. :)

Kishan Bheemajiyani
  • 3,241
  • 5
  • 30
  • 62
  • 1
    `List<>` is **not** backed by an array - it can be (as in an ArrayList). But since LinkedList is also a List (which is clearly **not** backed by an array) the statement "`List<>` is like an array which is making new array..." is plain wrong. – Thomas Kläger Feb 08 '16 at 17:14
  • List<> is basically backed by array that is why i said like that. So ya may be like wont be correct for that. But List its basically baked by array only. – Kishan Bheemajiyani Feb 09 '16 at 13:37
  • No, List<> is just an interface and has no implementation. How can it be backed by an array? Show me the implementation! – Thomas Kläger Feb 09 '16 at 14:19
  • if u create arraylist from that list instance. will it not be backed by array? – Kishan Bheemajiyani Feb 09 '16 at 14:44