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.

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.

Other Java Tutorials you may like