Sai A Sai A
Updated date Jul 17, 2023
In this blog, we will explore the step-by-step process of converting a Deque to a LinkedList in Java. It presents multiple methods, including manual conversion, using the LinkedList constructor, and leveraging the addAll() method.

Introduction:

In Java, the Deque interface provides a double-ended queue that allows elements to be inserted and removed from both ends. On the other hand, LinkedList is a doubly linked list implementation that allows for efficient insertion and removal operations. In some scenarios, you may need to convert a Deque to a LinkedList to take advantage of specific LinkedList functionalities. This blog will guide you through the process of converting a Deque to a LinkedList in Java, providing a detailed step-by-step tutorial.

Method 1: Manual Conversion

The first approach to converting a Deque to a LinkedList involves manually iterating over the Deque and adding its elements to a new LinkedList. Here's an example program that demonstrates this method:

import java.util.Deque;
import java.util.LinkedList;

public class DequeToLinkedListConverter {
    public static void main(String[] args) {
        // Create a Deque
        Deque<String> deque = new LinkedList<>();
        deque.add("Element 1");
        deque.add("Element 2");
        deque.add("Element 3");

        // Convert Deque to LinkedList
        LinkedList<String> linkedList = new LinkedList<>();
        for (String element : deque) {
            linkedList.add(element);
        }

        // Output the converted LinkedList
        System.out.println("LinkedList elements: " + linkedList);
    }
}

Output:

LinkedList elements: [Element 1, Element 2, Element 3]

In this method, we create a new LinkedList and iterate over the elements of the Deque using a foreach loop. During each iteration, we add each element to the LinkedList using the add() method. Finally, we output the converted LinkedList.

Method 2: Using the LinkedList Constructor

An alternative approach to converting a Deque to a LinkedList is to utilize the LinkedList constructor that accepts a Collection as a parameter. Here's an example:

import java.util.Deque;
import java.util.LinkedList;

public class DequeToLinkedListConverter {
    public static void main(String[] args) {
        // Create a Deque
        Deque<String> deque = new LinkedList<>();
        deque.add("Element 1");
        deque.add("Element 2");
        deque.add("Element 3");

        // Convert Deque to LinkedList
        LinkedList<String> linkedList = new LinkedList<>(deque);

        // Output the converted LinkedList
        System.out.println("LinkedList elements: " + linkedList);
    }
}

Output:

LinkedList elements: [Element 1, Element 2, Element 3]

In this method, we directly pass the Deque to the LinkedList constructor, which creates a new LinkedList with the elements of the Deque. The LinkedList constructor automatically iterates over the Deque and adds its elements to the new LinkedList.

Method 3: Using the addAll() Method

Another approach is to use the addAll() method provided by the LinkedList class. This method allows you to add all elements from the Deque to the LinkedList in a single step. Here's an example:

import java.util.Deque;
import java.util.LinkedList;

public class DequeToLinkedListConverter {
    public static void main(String[] args) {
        // Create a Deque
        Deque<String> deque = new LinkedList<>();
        deque.add("Element 1");
        deque.add("Element 2");
        deque.add("Element 3");

        // Convert Deque to LinkedList
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.addAll(deque);

        // Output the converted LinkedList
        System.out.println("LinkedList elements: " + linkedList);
    }
}

Output:

LinkedList elements: [Element 1, Element 2, Element 3]

In this method, we create a new LinkedList and use the addAll() method to add all elements from the Deque to the LinkedList. The addAll() method internally iterates over the Deque and adds its elements to the LinkedList.

Conclusion:

In this blog, we explored various methods to convert a Deque to a LinkedList in Java. We covered the manual conversion by iterating over the Deque and adding elements to the LinkedList, using the LinkedList constructor, and utilizing the addAll() method.

Comments (0)

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