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.

Download  Run Code

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.

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.

4. Using Guava Library

Guava also provides a LinkedList implementation, which can create an empty LinkedList instance.

5. Conversion between incompatible types

If ArrayList and LinkedList is expected to have incompatible types, we’ll need to convert them manually:

We can do better in Java 8 and above, as demonstrated below:

Or even better,

That’s all about converting ArrayList to LinkedList in Java.