This example shows how to add elements to LinkedList in Java using the add method of the LinkedList class. The example also shows how to add all elements of another Collection to the LinkedList object using the addAll method.
How to add elements to the LinkedList in Java?
There are several ways using which we can add elements to the LinkedList as given below.
1. Using the add method
The add
method of the LinkedList class adds the specified element to the linked list object.
1 |
public boolean add(E e) |
The add
method appends the specified element at the end of the linked list and returns true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.LinkedList; public class LinkedListAddElementsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListNumbers = new LinkedList<String>(); /* * To add an element to the LinkedList, use * the add method. * * The add method appends the element at the end * of the linked list */ //this will add "One" at the end of the linked list System.out.println( linkedListNumbers.add("One") ); //this will add "Two" at the end of the linked list System.out.println( linkedListNumbers.add("Two") ); System.out.println("LinkedList contains: " + linkedListNumbers); } } |
Output
1 2 3 |
true true LinkedList contains: [One, Two] |
As you can see from the output, the add
method appends the specified element at the end of the linked list.
How to insert an element in the LinkedList?
What if you want to insert an element to the LinkedList at the specified position? In that case, use the overloaded add
method that accepts an index at which you want to insert the element in the list.
1 |
public void add(int index, E element) |
Note: The return type of this overloaded add
method is void. It inserts an element at the specified index and shifts the subsequent elements to the right by adding 1 to their indices.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedList; public class LinkedListAddElementsExample { public static void main(String[] args) { //create new LinkedList object LinkedList<String> linkedListNumbers = new LinkedList<String>(); //add elements linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); /* * To insert an element at the specified index in the LinkedList, * use the overloaded add method and specify the index and element. */ //this will insert "Eleven" at index 1 linkedListNumbers.add(1, "Eleven"); System.out.println("LinkedList contains: " + linkedListNumbers); } } |
Output
1 |
LinkedList contains: [One, Eleven, Two, Three] |
2. Using the addFirst and addLast methods
The addFirst
and addLast
methods insert the specified element at the start and end of the linked list respectively.
1 |
public void addFirst(E e) |
1 |
public void addLast(E e) |
The return type of both of these methods is void.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); /* * To insert an element at the start of the list, use * the addFirst method */ //this will insert "Zero" at the start of the linked list linkedListNumbers.addFirst("Zero"); //this will append "Four" at the end of the linked list linkedListNumbers.addLast("Four"); System.out.println("LinkedList contains: " + linkedListNumbers); |
Output
1 |
LinkedList contains: [Zero, One, Two, Three, Four] |
Please visit how to insert an element at the beginning of the LinkedList example to know more.
Note: The addLast
and add
methods are similar because both of them append the specified element at the end of the linked list object.
3. Using the addAll method
The addAll
method of the LinkedList class adds all the elements of the specified Collection to this linked list object.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method appends all the elements of the specified collection object at the end of the linked list and returns true if this linked list object is changed after this method call.
Since the addAll
method accepts the Collection, I am going to show you how to add all elements of an ArrayList to the LinkedList in below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); ArrayList<String> aListOtherNumbes = new ArrayList<String>(); aListOtherNumbes.add("Four"); aListOtherNumbes.add("Five"); aListOtherNumbes.add("Six"); /* * To add all elements of ArrayList (or any other collection) * to the LinkedList, use the addAll method */ linkedListNumbers.addAll(aListOtherNumbes); System.out.println("LinkedList contains: " + linkedListNumbers); |
Output
1 |
LinkedList contains: [One, Two, Three, Four, Five, Six] |
As you can see from the output, all the elements of an ArrayList are appended to the LinkedList object at the end. If you want to insert all the elements of the Collection to the linked list at the specified position, use the below given overloaded addAll
method.
1 |
public boolean addAll(int index, Collection<? extends E> collection) |
This method inserts all the elements of the specified collection to the linked list object at the given index and shifts existing subsequent elements to the right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LinkedList<String> linkedListNumbers = new LinkedList<String>(); linkedListNumbers.add("One"); linkedListNumbers.add("Two"); linkedListNumbers.add("Three"); ArrayList<String> aListOtherNumbes = new ArrayList<String>(); aListOtherNumbes.add("Four"); aListOtherNumbes.add("Five"); aListOtherNumbes.add("Six"); /* * This will insert all elements of the ArrayList to * the LinkedList starting from index 0 */ linkedListNumbers.addAll(0, aListOtherNumbes); System.out.println("LinkedList contains: " + linkedListNumbers); |
Output
1 |
LinkedList contains: [Four, Five, Six, One, Two, Three] |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList