Java Program to Convert ArrayList to LinkedList
Last Updated :
28 Oct, 2021
Given an array list, your task is to write a program to convert the given array list to Linked List in Java.
Examples:
Input: ArrayList: [Geeks, forGeeks, A computer Portal]
Output: LinkedList: [Geeks, forGeeks, A computer Portal]
Input: ArrayList: [1, 2, 3, 4, 5]
Output: LinkedList: [1, 2, 3, 4, 5]
ArrayList – An ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
Linked List – A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:

Approaches
There are numerous approaches to convert the given array list to a linked list in Java. A few of them are listed below.
- Using Brute Force or Naive Method
- Using List Constructor
- Using Java 8 Streams API
- Using Google’s Guava Library
- Conversion between incompatible types
1. Using Brute Force or Naive Method
In this method, an empty LinkedList is created and all elements present of the ArrayList are added to it one by one.
Algorithm:
- Get the ArrayList to be converted.
- Create an empty LinkedList.
- Iterate through the items in the ArrayList.
- For each item, add it to LinkedList.
- Return the formed LinkedList.
Code:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertALtoLL(List<T> aL)
{
List<T> lL = new LinkedList<>();
for (T t : aL) {
lL.add(t);
}
return lL;
}
public static void main(String args[])
{
List<String> aL = Arrays.asList( "Geeks" ,
"forGeeks" ,
"A computer Portal" );
System.out.println( "ArrayList: " + aL);
List<String>
lL = convertALtoLL(aL);
System.out.println( "LinkedList: " + lL);
}
}
|
Output
ArrayList: [Geeks, forGeeks, A computer Portal]
LinkedList: [Geeks, forGeeks, A computer Portal]
2. Using List Constructor
In this method, the ArrayList is passed as the parameter into the LinkedList constructor.
Algorithm:
- Get the ArrayList to be converted.
- Create the LinkedList by passing the ArrayList as parameter in the constructor of the LinkedList.
- Return the formed LinkedList.
Code:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertALtoLL(List<T> aL)
{
List<T> lL = new LinkedList<>(aL);
return lL;
}
public static void main(String args[])
{
List<String> aL = Arrays.asList( "Geeks" ,
"forGeeks" ,
"A computer Portal" );
System.out.println( "ArrayList: " + aL);
List<String>
lL = convertALtoLL(aL);
System.out.println( "LinkedList: " + lL);
}
}
|
Output
ArrayList: [Geeks, forGeeks, A computer Portal]
LinkedList: [Geeks, forGeeks, A computer Portal]
3. Using Java 8 Stream API
This method includes converting the ArrayList to a Stream and collect elements of a stream in a LinkedList using Stream.collect() method which accepts a collector.
Algorithm:
- Get the ArrayList to be converted.
- Convert the ArrayList into the stream.
- Using Collectors, collect the ArrayList Stream and convert it into LinkedList.
- Now collect the LinkedList.
- Return the formed LinkedList.
Code:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertALtoLL(
List<T> aL)
{
return aL
.stream()
.collect(Collectors
.toCollection(LinkedList:: new ));
}
public static void main(String args[])
{
List<String> aL = Arrays.asList( "Geeks" ,
"forGeeks" ,
"A computer Portal" );
System.out.println( "ArrayList: " + aL);
List<String> lL = convertALtoLL(aL);
System.out.println( "LinkedList: " + lL);
}
}
|
Output
ArrayList: [Geeks, forGeeks, A computer Portal]
LinkedList: [Geeks, forGeeks, A computer Portal]
4. Using Google’s Guava library
Guava also provides a LinkedList implementation which can be used to create a LinkedList from another collection using Collection.addAll() method.
Algorithm:
- Get the ArrayList to be converted.
- Create an empty LinkedList.
- Add the elements of the ArrayList into the LinkedList using LinkedList.addAll() method and passing the ArrayList as the parameter.
- Return the formed LinkedList.
Code:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<T> convertALtoLL(List<T> aL)
{
List<T> lL = new LinkedList<>();
lL.addAll(aL);
return lL;
}
public static void main(String args[])
{
List<String> aL = Arrays.asList( "Geeks" ,
"forGeeks" ,
"A computer Portal" );
System.out.println( "ArrayList: " + aL);
List<String>
lL = convertALtoLL(aL);
System.out.println( "LinkedList: " + lL);
}
}
|
Output
ArrayList: [Geeks, forGeeks, A computer Portal]
LinkedList: [Geeks, forGeeks, A computer Portal]
5. Conversion between incompatible types
This method can be used if the required TreeMap is of the different type than the HashMap. In this, the conversion needs to be done manually.
Algorithm:
- Get the ArrayList to be converted.
- Convert the ArrayList into the stream.
- Convert the stream elements into the desired type by casting. This can be done by passing the casting function as parameter to map() function.
- Using Collectors, collect the ArrayList Stream and convert it into LinkedList.
- Now collect the LinkedList.
- Return the formed LinkedList.
Code:
Java
import java.util.*;
import java.util.stream.*;
class GFG {
public static <T> List<String> convertALtoLL(List<T> aL)
{
return aL
.stream()
.map(String::valueOf)
.collect(Collectors
.toCollection(LinkedList:: new ));
}
public static void main(String args[])
{
List<Integer> aL = Arrays.asList( 1 , 2 , 3 , 4 , 5 );
System.out.println( "ArrayList: " + aL);
List<String> lL = convertALtoLL(aL);
System.out.println( "LinkedList: " + lL);
}
}
|
Output
ArrayList: [1, 2, 3, 4, 5]
LinkedList: [1, 2, 3, 4, 5]
Similar Reads
How to convert LinkedList to Array in Java?
Given a Linked List in Java, the task is to convert this LinkedList to Array. Examples: Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5] Approach: Get the LinkedListConvert the LinkedList to Object
2 min read
Program to Convert List to Map in Java
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack class
5 min read
ArrayList vs LinkedList in Java
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. However, the limitation of the array is that the size of the array is predefined and fixed. There are multiple ways to solve this problem. In this article, the diff
5 min read
How to Convert HashMap to ArrayList in Java?
In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as: Extracting only the keys or values in the list form.Converting key-value pa
3 min read
Convert Vector to ArrayList in Java
There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right
3 min read
Convert List to Array in Java
The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give
5 min read
Convert List of Characters to String in Java
Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are
4 min read
LinkedList set() Method in Java
The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: Link
3 min read
LinkedList addAll() Method in Java
In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an
3 min read
ArrayList and LinkedList remove() methods in Java with Examples
List interface in Java (which is implemented by ArrayList and LinkedList) provides two versions of remove method. boolean remove(Object obj) : It accepts object to be removed. It returns true if it finds and removes the element. It returns false if the element to be removed is not present. Removes t
3 min read