This example shows how to add an array to LinkedList in Java. This example also shows various ways to add an array to LinkedList using asList and addAll methods.
How to add an array to LinkedList in Java?
There are a couple of ways using which you can add an array to the LinkedList object in Java.
1. Using the asList method of the Arrays class
The LinkedList class does not provide any direct method to add all elements of an array. However, we can first convert the array to a List and then add all elements of the List to the linked list.
To convert an array to a List object, we can use the asList
method of the Java Arrays class.
1 |
public static <T> List<T> asList(T... a) |
The asList
method returns a fixed-size list backed by the original array. Once we get a List from the array, we can then add all elements of the list to a linked list using the addAll
method of the LinkedList class.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all the elements of the specified collection to this linked list object.
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 27 28 29 30 31 32 |
import java.util.Arrays; import java.util.LinkedList; import java.util.List; public class LinkedListAddArrayExample { public static void main(String[] args) { LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); //array to be added to the list Integer[] intArray = {4, 5, 6}; /* * First, convert an array to the List * using the asList method */ List<Integer> list = Arrays.asList(intArray); /* * Add all elements of the List to the * linked list using the addAll method */ linkedListNumbers.addAll(list); System.out.println("LinkedList contains: " + linkedListNumbers); } } |
Output
1 |
LinkedList contains: [1, 2, 3, 4, 5, 6] |
As you can see from the output, all elements of an array are appended to the end of the linked list object.
2. Using the addAll method of the Collections class
You can also use the addAll
method of the Collections class.
1 |
public static <T> boolean addAll(Collection<? super T> collection, T... elements) |
The addAll
method returns true if the specified collection is changed due to this method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); //array to be added to the list Integer[] intArray = {4, 5, 6}; /* * To add an array to the linked list, use * the addAll method of the Collections class */ Collections.addAll(linkedListNumbers, intArray); System.out.println("LinkedList contains: " + linkedListNumbers); |
Output
1 |
LinkedList contains: [1, 2, 3, 4, 5, 6] |
What is the preferred way?
The addAll
method of the Collections class is the preferred way to add all elements of an array to the linked list object. Since it does not involve an additional step of converting an array to the list, it should perform better than the first approach.
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