Convert ArrayList to LinkedList in Java
This post will discuss how to convert ArrayList to LinkedList in Java. The resultant LinkedList should contain all elements of the ArrayList.
We know that ArrayList is a resizable-array implementation and LinkedList is a doubly-linked list implementation of the List interface.
ArrayList is usually faster than LinkedList as it offers constant-time positional access versus linear-time in a LinkedList. But LinkedList offers few constant-time operations such as adding elements at the beginning of the list or iterate over the list to delete elements from its interior instead of linear-time in an ArrayList.
Following are a few ways to convert ArrayList to LinkedList in Java:
1. Naive Solution
A naive solution is to create an empty LinkedList instance and add all elements present in the ArrayList to it one by one.
|
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 |
import java.util.ArrayList; import java.util.List; import java.util.LinkedList; import java.util.Arrays; import java.util.stream.Collectors; class Main { // Generic method to construct a new `LinkedList` from `ArrayList` public static <T> List<T> getInstance(List<T> ArrayList) { List<T> linkedList = new LinkedList<>(); for (T e: ArrayList) { linkedList.add(e); } return linkedList; } // Program to convert `ArrayList` to `LinkedList` in Java public static void main(String[] args) { List<String> ArrayList = Arrays.asList("RED", "BLUE", "GREEN"); // construct a new `LinkedList` from `ArrayList` List<String> linkedList = getInstance(ArrayList); System.out.println(linkedList); } } |
Output:
[RED, BLUE, GREEN]
2. Plain Java
To construct a new LinkedList from ArrayList, we can either pass ArrayList object to LinkedList constructor or to addAll() method.
|
1 2 3 4 5 6 7 8 |
// Generic method to construct a new `LinkedList` from `ArrayList` public static <T> List<T> getInstance(List<T> ArrayList) { List<T> linkedList = new LinkedList<>(); linkedList.addAll(ArrayList); return linkedList; } |
3. Using Java 8
The idea is to convert ArrayList to a stream and collect elements of a stream in a LinkedList using the Stream.collect() method, which accepts a collector.
We can use collector returned by Collectors.toCollection() method that can accept desired constructor reference LinkedList::new.
|
1 2 3 4 5 6 |
// Generic method to construct a new `LinkedList` from `ArrayList` public static <T> List<T> getInstance(List<T> ArrayList) { return ArrayList.stream() .collect(Collectors.toCollection(LinkedList::new)); } |
4. Using Guava Library
Guava also provides a LinkedList implementation, which can create an empty LinkedList instance.
|
1 2 3 4 5 6 7 8 |
// Generic method to construct a new `LinkedList` from `ArrayList` public static <T> List<T> getInstance(List<T> ArrayList) { List<T> linkedList = Lists.newLinkedList(); linkedList.addAll(ArrayList); return linkedList; } |
5. Conversion between incompatible types
If ArrayList and LinkedList is expected to have incompatible types, we’ll need to convert them manually:
|
1 2 3 4 5 6 7 8 9 10 11 |
// Construct a new `LinkedList<String>` from `ArrayList<Integer>` public static List<String> getInstance(List<Integer> ArrayList) { List<String> linkedList = new LinkedList<>(); for (Integer i: ArrayList) { linkedList.add(String.valueOf(i)); } return linkedList; } |
We can do better in Java 8 and above, as demonstrated below:
|
1 2 3 4 5 6 7 |
// Construct a new `LinkedList<String>` from `ArrayList<Integer>` in Java 8 and above public static List<String> getInstance(List<Integer> ArrayList) { return ArrayList.stream() .map(String::valueOf) .collect(Collectors.toCollection(LinkedList::new)); } |
Or even better,
|
1 2 3 4 5 6 7 8 9 10 |
// Construct a new `LinkedList<String>` from `ArrayList<Integer>` in Java 8 and above public static List<String> getInstance(List<Integer> ArrayList) { List<String> linkedList = new LinkedList<>(); ArrayList.stream() .map(String::valueOf) .forEach(linkedList::add); return linkedList; } |
That’s all about converting ArrayList to LinkedList in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)