Java LinkedList is an implementation of the List and Deque interfaces. It is one of the frequently used List implementation class. It extends AbstractSequentialList and implements List and Deque interfaces. It is an ordered collection and supports duplicate elements. It stores elements in Insertion order. It supports adding null elements. It supports index based operations. If you want to learn more about List basics, please go through this post: Java List.
In this post we are going to this discuss the following concepts.
In this section, we will discuss some of the important points about Java LinkedList:
As we know, Java LinkedList is one the List implementation class. It also implements Deque. As shown in class diagram below, it does NOT extends directly from AbstractList class. It extends AbstractSequentialList class.
In this section we will discuss some of the useful and frequently used Java LinkedList methods. The following methods are inherited from List or Collection interface:
The following methods are specific to LinkedList class which are inherited from Deque interface:
In this section, we will discuss about Java LinkedList basic example. We will explore some more useful operations in the coming sections. Example:-
import java.util.LinkedList;
import java.util.List;
public class LinkedListDemo
{
public static void main(String[] args)
{
List names = new LinkedList();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
names.add(2011);
System.out.println("LinkedList content: " + names);
System.out.println("LinkedList size: " + names.size());
}
}
Output:-
LinkedList content: [Rams, Posa, Chinni, 2011]
LinkedList size: 4
Here we have created a LinkedList object and added 4 items. As we discussed LinkedList.size() method is used to get the number of elements in the list. NOTE:- Without using Generics, Java LinkedList supports Heterogeneous elements. However, it is not recommended to use Collections without Generics. Let us explore Java Generics Advantages and usage in the coming section with one simple example.
In this section, we will discuss on how to use Generics with Java LinkedList. As we know, Java Generics are useful to write Type Safety programming and do Stronger type checks at compile time. They are also useful to eliminate the casting overhead. Example:-
import java.util.LinkedList;
import java.util.List;
public class LinkedListGenericsDemo
{
public static void main(String[] args)
{
List<String> names = new LinkedList<>();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// We cannot add other than Strings
// names.add(2011);
System.out.println("LinkedList content: " + names);
System.out.println("LinkedList size: " + names.size());
}
}
Output:-
LinkedList content: [Rams, Posa, Chinni]
LinkedList size: 3
Here we have created a LinkedList object with Generics and added 3 items. When we try to add a Number for LinkedList, it throws compile-time error.
In this section, we will explore how to convert a Java Array to LinkedList object. We can do it in many ways, however I have given only one approach here. Example:-
import java.util.LinkedList;
import java.util.List;
public class JavaArrayToLinkedListDemo
{
public static void main(String[] args)
{
Integer[] numbers = {1,2,3,4,5};
List<Integer> numbersList = new LinkedList<>();
for(Integer s : numbers){
numbersList.add(s);
}
System.out.println(numbersList);
}
}
Output:-
[1, 2, 3, 4, 5]
In this section, we will explore how to convert a Java LinkedList to an Array. We can do it in many ways, however I have given only one approach here. Example:-
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class LinkedListToJavaArrayDemo
{
public static void main(String[] args)
{
List<Integer> numbersList = new LinkedList<>();
numbersList.add(1);
numbersList.add(2);
numbersList.add(3);
numbersList.add(4);
numbersList.add(5);
Integer[] numbers = new Integer[numbersList.size()];
numbers = numbersList.toArray(numbers);
System.out.println(Arrays.toString(numbers));
}
}
Output:-
[1, 2, 3, 4, 5]
In this section, we will discuss about what is the best and what is the worst case scenarios to use LinkedList in Java applications. Best Usecase scenario:-
Why? Because we don’t need to do more shifts to add or remove elements at the middle of the list. Please refer “How Insertion works in J Java LinkedList?” section to understand it in-detail. Worst Usecase scenario:-
Why? Because LinkedList supports only sequential access, does NOT support random access. Please refer “How Deletion works in J Java LinkedList?” section to understand it in-detail. NOTE:- LinkedList implements List, Deque, Cloneable and Serializable. But it does NOT implement RandomAccess interface.
As we know, internally Java LinkedList is implemented using Doubly Linked List. So Java LinkedList represents it’s elements as Nodes. Each Node is divided into 3 portions as shown below. Here each Node is used for a specific purpose.
NOTE:- In JVM, LinkedList does NOT store it’s elements in consecutive order. It stores it’s elements at any available space and they are connected each other using Left and Right side Node portions as shown in the below diagram.
We have already seen how LinkedList stores it’s elements as Nodes in the previous section. In this section, we will discuss about how Java LinkedList’s Insertion operation works internally.
linkedList.add(2,54);
Here we are trying to perform Insertion operation to add new element with value “54” at index 2.5. Updated LinkedList looks like below.
We have already seen how LinkedList performs Insertion operation internally in the previous section. In this section, we will discuss about how Java LinkedList’s Deletion operation works internally.
linkedList.remove(3);
Here we are trying to perform Deletion operation to delete an element which is available at index 3.5. Updated LinkedList looks like below.
Here we will explore how a LinkedList object works as a Deque. We use these operations to implement Queues or Stacks. We will discuss how a Stack or Queues works in-depth in my coming posts. Example:-
import java.util.LinkedList;
import java.util.LinkedList;
import java.util.Deque;
public class LinkedListDequeOperationsDemo
{
public static void main(String[] args)
{
Deque names = new LinkedList();
names.add(2);
names.addFirst(1);
names.addLast(3);
names.addFirst(0);
names.addLast(4);
System.out.println("LinkedList content: " + names);
System.out.println("LinkedList size: " + names.size());
names.removeFirst();
names.removeLast();
System.out.println("LinkedList content: " + names);
System.out.println("LinkedList size: " + names.size());
}
}
Output:-
LinkedList content: [0, 1, 2, 3, 4]
LinkedList size: 5
LinkedList content: [1, 2, 3]
LinkedList size: 3
Here we will explore how to convert a LinkedList object to Java SE 8 Stream concept. Example:-
import java.util.LinkedList;
import java.util.List;
public class LinkedListToStreamDemo
{
public static void main(String[] args)
{
List<Integer> numbersList = new LinkedList<>();
numbersList.add(1);
numbersList.add(2);
numbersList.add(3);
numbersList.add(4);
numbersList.add(5);
//convert List to stream
numbersList.stream().forEach(System.out::println);
}
}
Output:-
1
2
3
4
5
In Java SE 9, Oracle Corp is going to add some useful utility methods to create Immutable List. If you want to learn them in-depth with some useful examples, please go through my post at: Java SE 9: Factory Methods for Immutable List That’s all of a quick roundup on LinkedList in Java. I hope these Java LinkedList examples will help you in getting started with LinkedList programming. Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
instead of using 2 for loops to rotate whole array left for say 4 times (which is of course have time-complexity issue takes of compiler time ,so its not a good solution) is it good to use linked list for that ,if yes then why ,please tell
- vivek
LinkedList is a interface as your blog please explain below line: Java LinkedList interface is a member of the Java Collections Framework.
- jh
Hi Rambabu, Here I would like to mention a typo error in the bellow line. Java LinkedList interface is a member of the Java Collections Framework. LinkedList is not interface, it is a class. it will really confuse the readers, if they are new to Java.
- Savitha Chitla
really thanks for this post . As per you . It does not implement RandomAccess interface. So we can access elements in sequential order only. It does not support accessing elements randomly. but in the next paragraph i can see it supports get and set , if it supports get and set then how it cant support random access , r
- Rahul
Hi Rambabu p, It was great a article and given detailed ,unknown points like linked list extends AbstractSequentialList and implements Deque interfaces with an appropriate uml class diagrams but my suggestions are there is some typo mistakes in LinkedList Deque Methods last methods names are same , so could you please check once or twice before you posting because there are new to me ,chance to leads a confusion , anyway nice article keep post ,thanks for the post…thank you
- vickram