Sai A Sai A
Updated date Jul 13, 2023
In this blog, we will provide a complete guide on converting a LinkedList to a Deque in Java. It explores multiple methods with code examples and detailed explanations, allowing developers to leverage the additional functionalities offered by Deque.

Introduction:

In Java, the LinkedList and Deque are two commonly used data structures that offer different functionalities. While LinkedList provides efficient insertion and deletion at both ends, Deque (Double Ended Queue) allows elements to be added or removed from both ends. However, there may be scenarios where we need to convert a LinkedList into a Deque to take advantage of Deque's additional features. In this blog, we will explore multiple methods to convert a LinkedList to a Deque in Java, providing detailed explanations and examples along the way.

Method 1: Using the Deque Interface

The Deque interface extends the Queue interface, which LinkedList implements. Therefore, we can simply create a new Deque instance and initialize it with the LinkedList object. Here's an example program:

import java.util.*;

public class LinkedListToDequeExample {

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Element 1");
        linkedList.add("Element 2");
        linkedList.add("Element 3");

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

        System.out.println("Converted Deque: " + deque);
    }
}

Output:

Converted Deque: [Element 1, Element 2, Element 3]

In this method, we create a LinkedList and populate it with some elements. Then, we create a new Deque instance named deque and initialize it with the LinkedList object linkedList using the constructor LinkedList<>(linkedList). The elements from the LinkedList are copied to the Deque, resulting in a converted Deque object. Finally, we print the converted Deque to verify the conversion.

Method 2: Using the addAll() Method

Another approach to convert a LinkedList to a Deque is by using the addAll() method provided by the Deque interface. This method allows us to add all elements from a collection to the end of the Deque. Here's an example:

import java.util.*;

public class LinkedListToDequeExample {

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("Element 1");
        linkedList.add("Element 2");
        linkedList.add("Element 3");

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

        System.out.println("Converted Deque: " + deque);
    }
}

Output:

Converted Deque: [Element 1, Element 2, Element 3]

In this method, we start by creating a LinkedList and adding elements to it. Then, we create an empty Deque named deque. Using the addAll() method, we add all elements from the LinkedList to the Deque. The addAll() method adds the elements at the end of the Deque, effectively converting it into a Deque with the same elements. Finally, we print the converted Deque to verify the conversion.

Conclusion:

In this blog, we explored multiple methods to convert a LinkedList to a Deque in Java. We covered using the Deque interface, the addAll() method, and other techniques. By converting a LinkedList to a Deque, we gain access to the additional functionalities provided by the Deque interface.

Comments (0)

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