Open In App

Conversion of a List to a Queue in Java

Last Updated : 25 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, We will learn about how to convert a List to a Queue in Java using various methods.

Prerequisite

Methods to Convert List to Queue in Java

In Java, there are a few ways to convert a List to a Queue. Here are some methods and ways you can achieve this:

  1. Use LinkedList Constructor
  2. Use ArrayDeque
  3. Use addAll method

1. LinkedList Constructor

Using LinkedList Constructor, seamlessly convert a List into a Queue by relying on the constructor of the LinkedList class.

Steps for Conversion

  1. Create a List.
  2. Use LinkedList Constructor to Create a Queue Interface and pass a created list as an argument.
  3. Access Queue Elements.

Syntax:

Queue<_type_> myQueue = new LinkedList<>(_ListName_);

Below is the example code for the above method:

Java




// Java Program to Convert List to Queue
// Using LinkedList Constructor
import java.util.*;
  
// Driver Class
public class ListToQueueExample1 {
      // Main function
    public static void main(String[] args) {
        // Creating a List
        List<String> myList = Arrays.asList("A", "B", "C");
  
        // Using LinkedList Constructor to
        // Convert List to Queue
        Queue<String> myQueue = new LinkedList<>(myList);
  
        // Access elements in the Queue
        System.out.print("Queue elements: ");
  
        // Iterate over the elements and dequeue
        while (!myQueue.isEmpty()) {
            System.out.print(myQueue.poll()+", ");
        }
    }
}


Output

Queue elements: A, B, C, 

2. ArrayDeque

By employing the ArrayDeque constructor and passing the List as an argument, we can efficiently create a Queue with the same elements as the original List.

Steps for Conversion:

  1. Create a List.
  2. Use ArrayDeque Constructor to Create Queue Interface and pass created list as argument.
  3. Access Queue Elements.

Syntax:

Queue<_type_> myQueue = new ArrayDeque<>(_ListName_);

Below is the example code for the above method:

Java




// Java Program to Convert List to Queue
// Using ArrayDeque
import java.util.*;
  
// Driver Class
public class ListToQueueExample2 {
      // Main function
    public static void main(String[] args) {
        // Creating a List
        List<String> myList = Arrays.asList("A", "B", "C");
  
        // Using ArrayDeque Constructor to 
        // convert List to Queue
        Queue<String> myQueue = new ArrayDeque<>(myList);
  
        // Access elements in the Queue
        System.out.print("Queue elements: ");
  
        // Iterate over the elements and dequeue
        while (!myQueue.isEmpty()) {
            System.out.print(myQueue.poll()+", ");
        }
    }
}


Output

Queue elements: A, B, C, 

3. addAll Method

The addAll Method is particularly useful when you already have a Queue instance and want to append elements from a List to it.

Steps for Conversion:

  1. Create a List.
  2. Create an Existing Queue(Empty)
  3. Use addAll Method with the List containing all elements.

Syntax:

MyQueue.addAll(_ListName_);

Below is the example code for the above method:

Java




// Java Program to Convert List to Queue
// Using addAll Method
import java.util.*;
  
// Driver Class
public class ListToQueueExample3 {
    // Main Function
    public static void main(String[] args)
    {
        // Creating a List
        List<String> myList = List.of("A", "B", "C");
  
        // Creating an Existing Queue (LinkedList in this
        // case)
        Queue<String> myQueue = new LinkedList<>();
  
        // Using addAll Method to add all elements from List
        // to Queue
        myQueue.addAll(myList);
  
        // Access elements in the Queue
        System.out.print("Queue elements: ");
  
        // Iterate over the elements and dequeue
        while (!myQueue.isEmpty()) {
            System.out.print(myQueue.poll() + ", ");
        }
    }
}


Output

Queue elements: A, B, C, 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads