Converting ArrayList to LinkedList

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to convert an ArrayList to a LinkedList in Java. Both are classes used to implement a list, but ArrayList stores data in an array-like list while LinkedList uses a linked list data structure. We will go through several methods to convert an ArrayList to LinkedList.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("Java")) -.-> java/BasicSyntaxGroup(["Basic Syntax"]) java(("Java")) -.-> java/DataStructuresGroup(["Data Structures"]) java(("Java")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["Object-Oriented and Advanced Concepts"]) java(("Java")) -.-> java/FileandIOManagementGroup(["File and I/O Management"]) java/BasicSyntaxGroup -.-> java/output("Output") java/DataStructuresGroup -.-> java/collections_methods("Collections Methods") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/constructors("Constructors") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/arraylist("ArrayList") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/linkedlist("LinkedList") java/FileandIOManagementGroup -.-> java/stream("Stream") subgraph Lab Skills java/output -.-> lab-117417{{"Converting ArrayList to LinkedList"}} java/collections_methods -.-> lab-117417{{"Converting ArrayList to LinkedList"}} java/constructors -.-> lab-117417{{"Converting ArrayList to LinkedList"}} java/arraylist -.-> lab-117417{{"Converting ArrayList to LinkedList"}} java/linkedlist -.-> lab-117417{{"Converting ArrayList to LinkedList"}} java/stream -.-> lab-117417{{"Converting ArrayList to LinkedList"}} end

Create an ArrayList

We will start by creating an ArrayList containing string elements.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Mango");
        arrayList.add("Apple");
        arrayList.add("Orange");
        System.out.println(arrayList);
    }
}

To run the code, open your terminal and navigate to the directory containing your java file. Then, compile and run the code using the following command:

javac Main.java && java Main

You should see the output:

[Mango, Apple, Orange]

Use a constructor to convert ArrayList to LinkedList

The easiest way to convert an ArrayList to a LinkedList is to use a constructor. We can pass the ArrayList as an argument to the LinkedList constructor.

import java.util.ArrayList;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Mango");
        arrayList.add("Apple");
        arrayList.add("Orange");
        System.out.println("ArrayList: ");
        System.out.println(arrayList);

        // ArrayList to LinkedList
        System.out.println("LinkedList: ");
        LinkedList<String> linkedList = new LinkedList<>(arrayList);
        System.out.println(linkedList);
    }
}

To run the code, open your terminal and navigate to the directory containing your java file. Then, compile and run the code using the following command:

javac Main.java && java Main

You should see the output:

ArrayList:
[Mango, Apple, Orange]
LinkedList:
[Mango, Apple, Orange]

Use add() method to convert ArrayList to LinkedList

We can also add elements from the ArrayList to the LinkedList one by one using the add() method.

import java.util.ArrayList;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Mango");
        arrayList.add("Apple");
        arrayList.add("Orange");
        System.out.println("ArrayList: ");
        System.out.println(arrayList);

        // ArrayList to LinkedList
        System.out.println("LinkedList: ");
        LinkedList<String> linkedList = new LinkedList<>();
        for (String str : arrayList) {
            linkedList.add(str);
        }
        System.out.println(linkedList);
    }
}

To run the code, open your terminal and navigate to the directory containing your java file. Then, compile and run the code using the following command:

javac Main.java && java Main

You should see the output:

ArrayList:
[Mango, Apple, Orange]
LinkedList:
[Mango, Apple, Orange]

Use stream API to convert ArrayList to LinkedList

If you are using Java 8 or higher version, we can use Java Stream API to convert ArrayList to LinkedList. We can use the stream() method to get a stream of elements from the ArrayList and collect() method to collect the elements into a LinkedList.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("Mango");
        arrayList.add("Apple");
        arrayList.add("Orange");
        System.out.println("ArrayList: ");
        System.out.println(arrayList);

        // ArrayList to LinkedList using Java Stream API
        System.out.println("LinkedList: ");
        LinkedList<String> linkedList = arrayList.stream()
                .collect(Collectors.toCollection(LinkedList::new));
        System.out.println(linkedList);
    }
}

To run the code, open your terminal and navigate to the directory containing your java file. Then, compile and run the code using the following command:

javac Main.java && java Main

You should see the output:

ArrayList:
[Mango, Apple, Orange]
LinkedList:
[Mango, Apple, Orange]

Summary

In this lab, we learned different ways to convert an ArrayList to a LinkedList in Java, such as using a constructor, adding elements one by one, or using Java Stream API. You can use these methods based on your requirements in your project.