ArrayList is a part of 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.
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:
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]
Below are methods to convert ArrayList to LinkedList in Java:
- 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 the LinkedList
- Return the formed LinkedList
Program:
// Java Program to convert// ArrayList to LinkedListimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an ArrayList to LinkedListpublicstatic<T> List<T> convertALtoLL(List<T> aL){// Create an empty LinkedListList<T> lL =newLinkedList<>();// Iterate through the aLfor(T t : aL) {// Add each element into the lLlL.add(t);}// Return the converted LinkedListreturnlL;}publicstaticvoidmain(String args[]){// Create an ArrayListList<String> aL = Arrays.asList("Geeks","forGeeks","A computer Portal");// Print the ArrayListSystem.out.println("ArrayList: "+ aL);// convert the ArrayList to LinkedListList<String>lL = convertALtoLL(aL);// Print the LinkedListSystem.out.println("LinkedList: "+ lL);}}Output:ArrayList: [Geeks, forGeeks, A computer Portal] LinkedList: [Geeks, forGeeks, A computer Portal]
- Using Plain Java: 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
Program:
// Java Program to convert// ArrayList to LinkedListimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an ArrayList to LinkedListpublicstatic<T> List<T> convertALtoLL(List<T> aL){// Create the LinkedList by passing the ArrayList// as parameter in the constructorList<T> lL =newLinkedList<>(aL);// Return the converted LinkedListreturnlL;}publicstaticvoidmain(String args[]){// Create an ArrayListList<String> aL = Arrays.asList("Geeks","forGeeks","A computer Portal");// Print the ArrayListSystem.out.println("ArrayList: "+ aL);// convert the ArrayList to LinkedListList<String>lL = convertALtoLL(aL);// Print the LinkedListSystem.out.println("LinkedList: "+ lL);}}Output:ArrayList: [Geeks, forGeeks, A computer Portal] LinkedList: [Geeks, forGeeks, A computer Portal]
- 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 stream
- Using Collectors, collect the ArrayList Stream and convert it into LinkedList
- Now collect the LinkedList
- Return the formed LinkedList
Program:
// Java Program to convert// ArrayList to LinkedListimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an// ArrayList to LinkedListpublicstatic<T> List<T> convertALtoLL(List<T> aL){// Return the converted LinkedListreturnaL// Convert the ArrayList into Stream.stream()// Collect the LinkedList.collect(Collectors// Convert the Stream into LinkedList// Collection type.toCollection(LinkedList::new));}publicstaticvoidmain(String args[]){// Create an ArrayListList<String> aL = Arrays.asList("Geeks","forGeeks","A computer Portal");// Print the ArrayListSystem.out.println("ArrayList: "+ aL);// convert the ArrayList to LinkedListList<String> lL = convertALtoLL(aL);// Print the LinkedListSystem.out.println("LinkedList: "+ lL);}}Output:ArrayList: [Geeks, forGeeks, A computer Portal] LinkedList: [Geeks, forGeeks, A computer Portal]
- 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
Program:
// Java Program to convert// ArrayList to LinkedListimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an ArrayList// to LinkedListpublicstatic<T> List<T> convertALtoLL(List<T> aL){// Create an empty LinkedListList<T> lL =newLinkedList<>();// Add ArrayList into the lLlL.addAll(aL);// Return the converted LinkedListreturnlL;}publicstaticvoidmain(String args[]){// Create an ArrayListList<String> aL = Arrays.asList("Geeks","forGeeks","A computer Portal");// Print the ArrayListSystem.out.println("ArrayList: "+ aL);// convert the ArrayList to LinkedListList<String>lL = convertALtoLL(aL);// Print the LinkedListSystem.out.println("LinkedList: "+ lL);}}Output:ArrayList: [Geeks, forGeeks, A computer Portal] LinkedList: [Geeks, forGeeks, A computer Portal]
- 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 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
Program:
// Java Program to convert// ArrayList to LinkedListimportjava.util.*;importjava.util.stream.*;classGFG {// Generic function to convert an ArrayList to LinkedListpublicstatic<T> List<String> convertALtoLL(List<T> aL){// Return the converted LinkedListreturnaL// Convert the ArrayList into Stream.stream()// Convert the Stream into String// Desired casting function can be passed// as parameter in next step.map(String::valueOf)// Collect the LinkedList.collect(Collectors// Convert the Stream into LinkedList// Collection type.toCollection(LinkedList::new));}publicstaticvoidmain(String args[]){// Create an ArrayListList<Integer> aL = Arrays.asList(1,2,3,4,5);// Print the ArrayListSystem.out.println("ArrayList: "+ aL);// convert the ArrayList to LinkedListList<String> lL = convertALtoLL(aL);// Print the LinkedListSystem.out.println("LinkedList: "+ lL);}}Output:ArrayList: [1, 2, 3, 4, 5] LinkedList: [1, 2, 3, 4, 5]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


