Sai A Sai A
Updated date Jun 30, 2023
In this blog, we will explore different methods for converting an array to a LinkedList in Java. It provides step-by-step explanations and code examples for each method, along with the corresponding output.

Introduction:

In Java, arrays and linked lists are two commonly used data structures with different characteristics. Arrays offer fast random access but have a fixed size, while linked lists provide dynamic size and efficient insertion and deletion operations. There might be scenarios where we need to convert an array into a linked list to leverage the advantages of linked lists. In this blog, we will explore multiple methods to convert an array to a LinkedList in Java. We will provide detailed explanations and code examples for each method, along with their respective outputs.

Method 1: Using the addAll() Method

One straightforward approach to converting an array to a LinkedList is by utilizing the addAll() method provided by the java.util.LinkedList class. This method allows us to append all the elements of an array to the end of an existing LinkedList.

import java.util.LinkedList;
import java.util.Arrays;

public class ArrayToLinkedList {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Orange"};
        LinkedList<String> linkedList = new LinkedList<>(Arrays.asList(array));
        System.out.println(linkedList);
    }
}

Output:

[Apple, Banana, Orange]

In this method, we start by creating an array named array containing the elements we want to convert. We then utilize the Arrays.asList() method to convert the array to a List. Finally, we pass this List as an argument to the LinkedList constructor, which initializes a LinkedList containing all the elements of the array. The System.out.println() statement displays the resulting LinkedList.

Method 2: Manual Conversion

Another approach involves manually iterating over the array and adding each element to the LinkedList using the add() method.

import java.util.LinkedList;

public class ArrayToLinkedList {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Orange"};
        LinkedList<String> linkedList = new LinkedList<>();
        
        for (String element : array) {
            linkedList.add(element);
        }
        
        System.out.println(linkedList);
    }
}

Output:

[Apple, Banana, Orange]

Here, we declare an empty LinkedList named linkedList. Next, we loop through each element in the array using a for-each loop. Within the loop, we add each element to the LinkedList using the add() method. Finally, we print the resulting LinkedList.

Method 3: Using Java Streams

Java 8 introduced the Stream API, which provides a concise way to perform operations on collections. We can leverage this API to convert an array to a LinkedList.

import java.util.LinkedList;
import java.util.Arrays;

public class ArrayToLinkedList {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Orange"};
        LinkedList<String> linkedList = Arrays.stream(array)
                                              .collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
        System.out.println(linkedList);
    }
}

Output:

[Apple, Banana, Orange]

In this method, we first convert the array to a stream using Arrays.stream(array). Then, we use the collect() method, which allows us to accumulate elements into a mutable result container. Within the collect() method, we provide three arguments: LinkedList::new (supplier) creates a new LinkedList, LinkedList::add (accumulator) adds each element to the LinkedList, and LinkedList::addAll (combiner) appends the elements from multiple threads (not applicable in this case). Finally, the resulting LinkedList is printed.

Conclusion:

In this blog, we explored multiple methods to convert an array to a LinkedList in Java. We discussed using the addAll() method, manual conversion through iteration, and utilizing Java Streams. Each method provided the desired output, demonstrating different techniques for achieving the conversion. Depending on the specific requirements and preferences, developers can choose the most suitable method for their use case. Understanding these conversion techniques is valuable for efficiently manipulating data structures in Java.

Comments (0)

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