Sai A Sai A
Updated date Jul 11, 2023
In this blog, we will learn how to convert a Queue to LinkedList in Java using two different methods. This step-by-step tutorial provides detailed explanations and example code, allowing you to understand and implement the conversion process.

Introduction:

In Java, a Queue is an abstract data type that follows the First-In-First-Out (FIFO) principle. On the other hand, a LinkedList is a doubly-linked list implementation in Java that provides efficient insertion and deletion operations. In certain scenarios, you may need to convert a Queue to a LinkedList to leverage the additional features and flexibility provided by LinkedList. In this blog, we will explore two methods to convert a Queue to LinkedList in Java, along with detailed explanations and example code.

Method 1: Manual Conversion

The first method involves manually converting a Queue to a LinkedList by iterating over the elements of the Queue and adding them to the LinkedList one by one.

import java.util.LinkedList;
import java.util.Queue;

public class QueueToLinkedListConverter {
    public static LinkedList<Integer> convertQueueToLinkedList(Queue<Integer> queue) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        
        while (!queue.isEmpty()) {
            linkedList.add(queue.poll());
        }
        
        return linkedList;
    }

    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        
        LinkedList<Integer> linkedList = convertQueueToLinkedList(queue);
        System.out.println("LinkedList: " + linkedList);
    }
}

Output:

LinkedList: [1, 2, 3]

In this method, we create an empty LinkedList and iterate over the elements of the Queue using a while loop. We use the poll() method to remove and retrieve the head of the Queue and then add it to the LinkedList using the add() method. This process continues until the Queue becomes empty. Finally, we return the converted LinkedList.

Method 2: LinkedList Constructor

The second method involves using the LinkedList class's constructor that accepts a Collection as a parameter. This constructor automatically converts the elements of the given Collection into a LinkedList.

import java.util.LinkedList;
import java.util.Queue;

public class QueueToLinkedListConverter {
    public static LinkedList<Integer> convertQueueToLinkedList(Queue<Integer> queue) {
        return new LinkedList<>(queue);
    }

    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        
        LinkedList<Integer> linkedList = convertQueueToLinkedList(queue);
        System.out.println("LinkedList: " + linkedList);
    }
}

Output:

LinkedList: [1, 2, 3]

In this method, we simply pass the Queue as an argument to the LinkedList constructor, which automatically converts the elements of the Queue into a LinkedList. This provides a concise and efficient way to convert a Queue to a LinkedList.

Conclusion:

In this blog, we explored two methods to convert a Queue to LinkedList in Java. The first method involved manually iterating over the elements of the Queue and adding them to the LinkedList one by one. The second method utilized the LinkedList constructor that accepts a Collection as a parameter, providing a more concise approach. Both methods yielded the same output, which was a LinkedList containing the elements from the original Queue. Depending on the specific requirements and context of your application, you can choose the most suitable method for converting a Queue to LinkedList in Java.

Comments (0)

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