Sai A Sai A
Updated date Jun 30, 2023
In this blog, we will discover efficient methods to convert a LinkedList to an ArrayList in Java. This comprehensive blog explores manual conversion using loops, utilizing the addAll() method, and leveraging the Stream API.

Introduction:

In Java, LinkedList and ArrayList are popular data structures with distinct characteristics. LinkedList excels in insertion and deletion operations, while ArrayList provides efficient random access. There may arise situations where it becomes necessary to convert a LinkedList to an ArrayList or vice versa. In this blog post, we will explore multiple methods to convert a LinkedList to an ArrayList in Java, complete with code examples and output.

Method 1: Manual Conversion Using Loops

The first method involves manually converting the LinkedList to an ArrayList by iterating over each element of the LinkedList and adding it to the ArrayList individually.

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

public class LinkedListToArrayList {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Orange");

        ArrayList<String> arrayList = new ArrayList<>();
        for (String element : linkedList) {
            arrayList.add(element);
        }

        // Output
        System.out.println("LinkedList: " + linkedList);
        System.out.println("ArrayList: " + arrayList);
    }
}

Output:

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

In this approach, we create a new ArrayList and iterate over each element of the LinkedList using an enhanced for loop. Within the loop, we add each element to the ArrayList using the add() method.

Method 2: Using the addAll() Method

The second method takes advantage of the addAll() method provided by the ArrayList class to efficiently convert a LinkedList to an ArrayList.

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

public class LinkedListToArrayList {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Orange");

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.addAll(linkedList);

        // Output
        System.out.println("LinkedList: " + linkedList);
        System.out.println("ArrayList: " + arrayList);
    }
}

Output:

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

In this approach, we create an empty ArrayList and use the addAll() method to add all elements from the LinkedList to the ArrayList. The addAll() method internally iterates over the LinkedList and adds each element to the ArrayList efficiently.

Method 3: Using the Stream API

The third method utilizes the Stream API introduced in Java 8, providing a concise and expressive way to convert a LinkedList to an ArrayList.

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

public class LinkedListToArrayList {
    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Apple");
        linkedList.add("Banana");
        linkedList.add("Orange");

        ArrayList<String> arrayList = linkedList.stream()
                                                .collect(Collectors.toCollection(ArrayList::new));

        // Output
        System.out.println("LinkedList: " + linkedList);
        System.out.println("ArrayList: " + arrayList);
    }
}

Output:

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

In this method, we utilize the stream() method of the LinkedList to create a stream of elements. We then use the collect() method along with Collectors.toCollection() to convert the stream to an ArrayList efficiently.

Conclusion:

In this blog post, we explored multiple methods to convert a LinkedList to an ArrayList in Java. We discussed manual conversion using loops, utilizing the addAll() method, and leveraging the Stream API. Each method achieves the desired conversion, and the choice depends on the specific requirements of the application. By employing these techniques, you can effectively convert LinkedLists to ArrayLists, making it easier to work with data in different scenarios.

Comments (0)

There are no comments. Be the first to comment!!!