Program to convert ArrayList to LinkedList in Java

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:

  1. 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:

    1. Get the ArrayList to be converted.
    2. Create an empty LinkedList
    3. Iterate through the items in the ArrayList.
    4. For each item, add it to the LinkedList
    5. Return the formed LinkedList

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // ArrayList to LinkedList
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an ArrayList to LinkedList
        public static <T> List<T> convertALtoLL(List<T> aL)
        {
      
            // Create an empty LinkedList
            List<T> lL = new LinkedList<>();
      
            // Iterate through the aL
            for (T t : aL) {
      
                // Add each element into the lL
                lL.add(t);
            }
      
            // Return the converted LinkedList
            return lL;
        }
      
        public static void main(String args[])
        {
            // Create an ArrayList
            List<String> aL = Arrays.asList("Geeks",
                                           "forGeeks",
                                           "A computer Portal");
      
            // Print the ArrayList
            System.out.println("ArrayList: " + aL);
      
            // convert the ArrayList to LinkedList
            List<String>
                lL = convertALtoLL(aL);
      
            // Print the LinkedList
            System.out.println("LinkedList: " + lL);
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [Geeks, forGeeks, A computer Portal]
    LinkedList: [Geeks, forGeeks, A computer Portal]
    
  2. Using Plain Java: In this method, the ArrayList is passed as the parameter into the LinkedList constructor.

    Algorithm:

    1. Get the ArrayList to be converted.
    2. Create the LinkedList by passing the ArrayList as parameter in the constructor of the LinkedList.
    3. Return the formed LinkedList

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // ArrayList to LinkedList
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an ArrayList to LinkedList
        public static <T> List<T> convertALtoLL(List<T> aL)
        {
      
            // Create the LinkedList by passing the ArrayList
            // as parameter in the constructor
            List<T> lL = new LinkedList<>(aL);
      
            // Return the converted LinkedList
            return lL;
        }
      
        public static void main(String args[])
        {
            // Create an ArrayList
            List<String> aL = Arrays.asList("Geeks",
                                           "forGeeks",
                                           "A computer Portal");
      
            // Print the ArrayList
            System.out.println("ArrayList: " + aL);
      
            // convert the ArrayList to LinkedList
            List<String>
                lL = convertALtoLL(aL);
      
            // Print the LinkedList
            System.out.println("LinkedList: " + lL);
        }
    }

    chevron_right

    
    

    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:

    1. Get the ArrayList to be converted.
    2. Convert the ArrayList into stream
    3. Using Collectors, collect the ArrayList Stream and convert it into LinkedList
    4. Now collect the LinkedList
    5. Return the formed LinkedList

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // ArrayList to LinkedList
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an
        // ArrayList to LinkedList
        public static <T> List<T> convertALtoLL(
                                               List<T> aL)
        {
      
            // Return the converted LinkedList
            return aL
      
                // Convert the ArrayList into Stream
                .stream()
      
                // Collect the LinkedList
                .collect(Collectors
      
                             // Convert the Stream into LinkedList
                             // Collection type
                             .toCollection(LinkedList::new));
        }
      
        public static void main(String args[])
        {
            // Create an ArrayList
            List<String> aL = Arrays.asList("Geeks",
                                            "forGeeks",
                                            "A computer Portal");
      
            // Print the ArrayList
            System.out.println("ArrayList: " + aL);
      
            // convert the ArrayList to LinkedList
            List<String>  lL = convertALtoLL(aL);
      
            // Print the LinkedList
            System.out.println("LinkedList: " + lL);
        }
    }

    chevron_right

    
    

    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:

    1. Get the ArrayList to be converted.
    2. Create an empty LinkedList
    3. Add the elements of the ArrayList into the LinkedList using LinkedList.addAll() method and passing the ArrayList as the parameter.
    4. Return the formed LinkedList

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // ArrayList to LinkedList
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an ArrayList
        // to LinkedList
        public static <T> List<T> convertALtoLL(List<T> aL)
        {
      
            // Create an empty LinkedList
            List<T> lL = new LinkedList<>();
      
            // Add ArrayList into the lL
            lL.addAll(aL);
      
            // Return the converted LinkedList
            return lL;
        }
      
        public static void main(String args[])
        {
            // Create an ArrayList
            List<String> aL = Arrays.asList("Geeks",
                                           "forGeeks",
                                           "A computer Portal");
      
            // Print the ArrayList
            System.out.println("ArrayList: " + aL);
      
            // convert the ArrayList to LinkedList
            List<String>
                lL = convertALtoLL(aL);
      
            // Print the LinkedList
            System.out.println("LinkedList: " + lL);
        }
    }

    chevron_right

    
    

    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:

    1. Get the ArrayList to be converted.
    2. Convert the ArrayList into stream
    3. Convert the stream elements into the desired type by casting. This can be done by passing the casting function as parameter to map() function.
    4. Using Collectors, collect the ArrayList Stream and convert it into LinkedList
    5. Now collect the LinkedList
    6. Return the formed LinkedList

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to convert
    // ArrayList to LinkedList
      
    import java.util.*;
    import java.util.stream.*;
      
    class GFG {
      
        // Generic function to convert an ArrayList to LinkedList
        public static <T> List<String> convertALtoLL(List<T> aL)
        {
      
            // Return the converted LinkedList
            return aL
      
                // 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));
        }
      
        public static void main(String args[])
        {
            // Create an ArrayList
            List<Integer> aL = Arrays.asList(1, 2, 3, 4, 5);
      
            // Print the ArrayList
            System.out.println("ArrayList: " + aL);
      
            // convert the ArrayList to LinkedList
            List<String>  lL = convertALtoLL(aL);
      
            // Print the LinkedList
            System.out.println("LinkedList: " + lL);
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [1, 2, 3, 4, 5]
    LinkedList: [1, 2, 3, 4, 5]
    


My Personal Notes arrow_drop_up

Technical Content Engineer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.