Sai A Sai A
Updated date Jun 27, 2023
In this blog, we explore two efficient methods to convert an ArrayList to a LinkedList in Java. We provide step-by-step explanations, code examples, and their corresponding outputs.

Introduction:

In Java, both ArrayList and LinkedList are commonly used data structures. While ArrayList provides efficient random access and dynamic resizing, LinkedList offers better performance for frequent insertion and removal operations. There may be situations where we need to convert an ArrayList to a LinkedList or vice versa. In this blog post, we will explore two methods to convert an ArrayList to a LinkedList in Java. We will provide a step-by-step explanation of each method, accompanied by code examples and their corresponding outputs.

Method 1: Using LinkedList Constructor

The LinkedList class in Java provides a constructor that allows us to initialize a LinkedList with elements from another collection. We can leverage this constructor to convert an ArrayList to a LinkedList in a single line of code.

// Step 1: Create an ArrayList
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Orange");

// Step 2: Convert ArrayList to LinkedList
LinkedList<String> linkedList = new LinkedList<>(arrayList);

Output:

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

In this method, we first create an ArrayList called arrayList and populate it with some elements. Then, we pass the arrayList as an argument to the LinkedList constructor, which initializes a new LinkedList with the elements from the ArrayList. This constructor automatically adds each element from the ArrayList to the end of the LinkedList, resulting in a one-to-one conversion.

Method 2: Manual Conversion

If we want more control over the conversion process or need to perform additional operations during the conversion, we can manually convert the ArrayList to a LinkedList element by element.

// Step 1: Create an ArrayList
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(10);
arrayList.add(20);
arrayList.add(30);

// Step 2: Create an empty LinkedList
LinkedList<Integer> linkedList = new LinkedList<>();

// Step 3: Convert ArrayList to LinkedList
for (Integer element : arrayList) {
    linkedList.add(element);
}

Output:

ArrayList: [10, 20, 30]
LinkedList: [10, 20, 30]

In this method, we start by creating an ArrayList named arrayList and adding a few integer elements to it. Then, we create an empty LinkedList called linkedList. Next, we iterate through each element in the ArrayList using a for-each loop and add it to the LinkedList using the add() method. By adding each element individually, we ensure that the order of elements in the LinkedList matches that of the ArrayList.

Conclusion:

In this blog post, we explored two methods to convert an ArrayList to a LinkedList in Java. The first method utilized the LinkedList constructor, allowing us to convert an ArrayList into a LinkedList in a single line of code. The second method involved manual conversion, where we iterated over each element in the ArrayList and added them individually to the LinkedList. Both methods provided the desired output, but the choice between them depends on the specific requirements of the situation.

Comments (0)

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