
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a Queue to a List in Java
In order to convert a Queue to a List in Java, we can create an LinkedList and pass the Queue as an argument in the parameterized constructor of an ArrayList. This can be done as follows −
Queue q = new LinkedList(); List l = new ArrayList(q);
The quickest way is used to LinkedList in the first place which can be used both as a List and a Queue. This can be done as follows −
Queue q = new LinkedList(); List l = (List) q;
Let us see a program to convert a queue to a list −
Example
import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Example { public static void main(String[] args) { Queue q = new LinkedList(); q.add("Good"); q.add("Morning"); List l = (List) q; System.out.println(l); } }
Output
[Good, Morning]
Advertisements