Tutorial: Learn how to add elements to a List in Java! 🤔
In this Java Code Example, we’ll learn how to add elements to a List in Java via the constructor, by invoking the add and addAll methods, by calling the addFirst and addLast methods, and finally by invoking the push method.
The solutions present in this article are compatible with Java 8 unless explicitly noted otherwise.
This article is part of an introduction to programming using Java series which contains links to basic through advanced examples.
Since the java.util.List is an interface we need to pick an implementation for this tutorial and, in this case, we’ll explore how to add elements in linked list in Java.
How to add elements in a List in Java TOC
The LinkedList is dynamically sized thus making it an ideal choice when the number of elements is either unknown or changes frequently.
The LinkedList is especially efficient for insertions and deletions, as these operations only involve updating links between elements and not shifting elements as with what happens when the same operations take place when using an ArrayList.
LinkedList instances are thereby suitable for situations where frequent additions and removals are common.
There are trade-offs, however.
LinkedLists are less efficient for random access, as they require traversing the list to reach an element thus resulting in an O(n) time complexity compared to O(1) in an ArrayList.
Additionally, each LinkedList element requires extra memory for storing pointers, thereby leading to higher memory use.
Ultimately, the choice between a LinkedList and other data structures depends on the application’s specific requirements, balancing factors such as the types of operations performed with memory efficiency.
Finally, we’ll also take a look at some of the performance considerations when adding elements to a Java List.
This article was updated on May 27, 2025.
Run the add items to a List implementation in Java example
In this section we’ll introduce an example application and how to execute this demonstration using the tio.run online interpreter.
Below I’ve included a short script that demonstrates how to add elements to an instance of java.util.LinkedList using the addAll method.
Note the red pointer on the top left indicates how to execute the program and the red pointer on the bottom of the image indicates where the output can be found.
Do you need to hire an Interim CTO?
How to add List elements in Java using the LinkedList constructor
In this example, we pass an instance of java.util.Collection as a parameter to the LinkedList constructor.
This solution is appropriate for situations where we have an instance of Collection and the List has not been instantiated — if the list already exists then we’ll need to add elements using one of the instance methods available.
Below I’ve included an example of the line of code where the LinkedList constructor is called and the reference to the valueList is passed in.
Note that we must pass an instance of java.util.Collection if we’re to use the constructor to pass values into the LinkedList.
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] values = {
"Apples",
"Pears",
"Bananas",
"Grapes",
"Peaches",
"Strawberries"
};
List valueList = Arrays.asList(values);
List exampleList = new LinkedList(valueList);
System.out.println ("exampleList: " + exampleList);
}
}
The example script can be executed on tio.run, and I have a breakdown of this page below.
Here’s a description regarding what each pointer points to:
- red — Click the play icon to run the example.
- orange — Convert the String array to an immutable instance of List.
- green — Pass the immutable instance of List created in the previous step into the LinkedList constructor.
- blue — Contains the output and this shows the contents of the exampleList.
Use the add and addAll methods to insert elements into a Java List
We can use either the add and addAll methods to insert elements into a LinkedList in the Java Programming Language.
The add method can be used when adding a single item to any implementation of the Java List interface, and that includes the LinkedList.
The addAll method can be used when adding a Collection containing one or more elements to the LinkedList.
The example screenshot below demonstrates the respective add and addAll method signatures.
The GitHub gist is available regarding how to insert elements into a List in Java on the right side of this page.
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] values = {
"Apples",
"Pears",
"Bananas",
"Grapes",
"Peaches",
"Strawberries"
};
List valueList = Arrays.asList(values);
List exampleList = new LinkedList<>();
exampleList.addAll(valueList);
System.out.println ("exampleList: " + exampleList);
}
}
The add and addAll methods are defined in the java.util.List interface and hence all implementations of List will also have these methods.
The LinkedList addFirst and addLast convenience methods
When we need to insert elements into a LinkedList in Java we can use the addFirst and addLast convenience methods.
The the addFirst and addLast convenience methods are only available on classes that implement the Deque interface and this includes the LinkedList.
The addFirst and addLast convenience methods offer significant benefits for efficient element insertion.
The addFirst and addLast convenience methods allow for fast and straightforward additions at the beginning and end of the list, respectively, without the need for resizing or element shifting which would otherwise be required when using either simple arrays or instances of the Java ArrayList.
This aforementioned efficiency is due to the LinkedList structure, where such operations are performed in constant time (O(1)), involving only the modification of pointers.
The addFirst and addLast methods enhance code readability and simplify implementation, as they eliminate the need for index calculations.
The addFirst and addLast convenience methods are particularly advantageous in applications where the order of elements is important, such as with stacks and queues.
Utilizing the addFirst and addLast methods contributes to cleaner and more efficient code, optimizing operations in scenarios where frequent additions are made to either the beginning or end of the list.
In the next section, we’ll look at using the push method to append elements to a LinkedList in Java.
Insert elements into a List in Java using the push method
The push method is only available on classes that implement the Deque interface and this includes the LinkedList.
It is for this reason that line #19 is of type java.util.LinkedList and not java.util.List.
In this example, we can see that “Tomatoes” is pushed into the exampleList on line #23.
When the push method is invoked, “Tomatoes” is inserted into the LinkedList at position zero and everything else is shifted rightward.
We can run this example in our browser using Try It Online, which offers interpreters for a growing list of practical and recreational programming languages, including the Java Programming Language.
The TIO icon on the right contains a link to the example shown here.
Conclusion
This tutorial is based on research from the StackOverflow post entitled How to add elements of a Java8 stream into an existing List along with some observations discovered while doing keyword research using SEMrush.
See also the article on Java autoboxing performance which can have a negative impact on performance when adding an array of primitive values to an instance of one of the Java Collections classes, such as a LinkedList.
Frequently Asked Questions (FAQ)
This section includes frequently asked questions as it pertains to the Java Programming Language and various topics and components covered in this instructional.
A linked list in Java is a data structure that consists of a sequence of elements, each contained in a node. Nodes are chained together using pointers, with each node pointing to the next node in the sequence. The use of pointers allows for efficient insertion and deletion of elements as it does not require shifting elements, which is required when relying on simple arrays.
Linked lists in Java (java.util.LinkedList) are useful in scenarios where frequent insertion and deletion of elements is required, as these operations can be performed more efficiently than in arrays, because there’s no need to shift elements. Linked lists (java.util.LinkedList) are also useful when the size of the data structure needs to be dynamically allocated.
- The data part that stores the element’s value, and
- a reference (or pointer) to the next node in the sequence, facilitating the link in the list.
Further Reading
- Refer to the Groovy Scripting Examples category for other articles on this subject.
- See the Java Code Examples category for other articles on this subject.











