Sai A Sai A
Updated date Jul 10, 2023
In this blog, we will learn how to convert a LinkedList to a Queue in Java. Explore two different methods, complete with code examples and explanations. Discover how to leverage existing LinkedList methods or create a new Queue object using the LinkedList as a parameter.

Introduction:

In Java, both LinkedLists and Queues are fundamental data structures frequently used in programming. While a LinkedList provides flexibility for adding, removing, and manipulating elements, a Queue offers a specialized functionality for managing elements in a specific order. In this tutorial, we will explore two methods to convert a LinkedList to a Queue in Java. We will provide code examples, explanations, and the expected output for each method.

Method 1: Using LinkedList as the underlying data structure

In this approach, we can leverage the existing LinkedList class in Java and utilize its methods to simulate Queue behavior. We will add elements to the LinkedList using the add() method, and remove elements from the front using the remove() method. By doing so, we can achieve a Queue-like behavior with a LinkedList.

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

public class LinkedListToQueueExample {

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        
        // Adding elements to the LinkedList
        linkedList.add("Element 1");
        linkedList.add("Element 2");
        linkedList.add("Element 3");
        
        Queue<String> queue = linkedList; // Implicit conversion
        
        // Removing elements from the Queue
        String removedElement = queue.remove();
        System.out.println("Removed element: " + removedElement);
    }
}

Output:

Removed element: Element 1

In this method, we start by creating a LinkedList and adding elements to it. Next, we assign the LinkedList instance to a Queue variable, queue, utilizing implicit conversion. This conversion works because LinkedList implements the Queue interface in Java. Finally, we remove an element from the front of the queue using the remove() method and display the removed element.

Method 2: Using the LinkedList as a parameter for the Queue implementation

In this approach, we will create a new instance of the Queue implementation, specifically a LinkedList, and pass the existing LinkedList as a parameter during its initialization. This allows us to create a separate Queue object while maintaining the original LinkedList.

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

public class LinkedListToQueueExample {

    public static void main(String[] args) {
        LinkedList<String> linkedList = new LinkedList<>();
        
        // Adding elements to the LinkedList
        linkedList.add("Element 1");
        linkedList.add("Element 2");
        linkedList.add("Element 3");
        
        Queue<String> queue = new LinkedList<>(linkedList); // Initializing Queue with LinkedList
        
        // Removing elements from the Queue
        String removedElement = queue.remove();
        System.out.println("Removed element: " + removedElement);
    }
}

Output:

Removed element: Element 1

In this method, we begin by creating a LinkedList and adding elements to it. Next, we create a new LinkedList instance by passing the existing LinkedList as a parameter during its initialization. This effectively creates a separate Queue object while maintaining the original LinkedList. Finally, we remove an element from the front of the queue using the remove() method and display the removed element.

Conclusion:

In this comprehensive tutorial, we explored two methods to convert a LinkedList to a Queue in Java. We discussed utilizing the LinkedList class itself as a Queue by leveraging its methods and implicit conversion. Additionally, we demonstrated creating a new LinkedList object and passing the existing LinkedList as a parameter during initialization to create a separate Queue object. Both methods allow us to achieve Queue-like behavior while still retaining the flexibility of a LinkedList. It's important to choose the method that best suits your specific use case and requirements.

By converting a LinkedList to a Queue, you can take advantage of the specialized features provided by the Queue interface, such as First-In-First-Out (FIFO) ordering and convenient methods like remove(), peek(), and offer()

Comments (0)

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