Why LinkedList in Java is not a real Linked List?

You can not find a method in LinkedList that gives you next or previous object in the list

No, and that's entirely appropriate. The idea of "next item in a list" doesn't make sense on a list. It makes perfect sense for a node within a list, but that's not something which is exposed by the Java API. It's present internally, of course - just not exposed. If you want to iterate over the list, you use an iterator. You can still add at the start or end, remove from the start or end, and add/remove from the iterator.

While you certainly can conflate the concepts of "node" and "list" as your suggested sample code does, I don't think it's generally a good idea.

To put it another way: what are you trying to achieve that's causing you problems? I believe you should be able to do what you want using the public API - you may just not have noticed it all.


My question is why Java has called this data structure LinkedList, while it is not truly a linked list?

Because the implementation of it is a linked list. From the documentation:

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).

LinkedList is a List implemented via a linked-list, ArrayList is a List implemented using an array, etc. Which one you choose can matter in terms of runtime characteristics. For instance, from the LinkedList docs:

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.

So you know, for instance, that next on the Iterator you get from iterator or listIterator will be quite efficient, but that get by index will involve traversal.


Whether a collection is a linked list or an array list is not about its contract, but about its implementation. LinkedList is indeed a linked list by implementation, and a java.util.List by contract.

Where it shows is not its API, but its space/time complexity characteristics, which anyone familiar with a linked list can easily anticipate.

For reference, this is the actual implementation of a LinkedList node, quite a good match to your expectation:

957  private static class Node<E> {
958 E item;
959 Node<E> next;
960 Node<E> prev;
962 Node(Node<E> prev, E element, Node<E> next) {
963 this.item = element;
964 this.next = next;
965 this.prev = prev;
966 }
967 }