Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a Queue in Java

Tags:

java

queue

I have a class Queue that fully implements the Queue interface, however I have no idea on how to actually initialize the queue in my main Code.

Queue<T> q = ???

I have been searching the internet for the answer for 30+ minutes AND consulted the Java API docs, but I am outright stuck. I know this is a simple question, and because of that its driving me insane. Any help?

like image 638
jnel899 Avatar asked Feb 25 '26 06:02

jnel899


2 Answers

Queue is an interface. You can't instantiate an interface directly. Instead, choose an existing implementation. For example:

Queue<Integer> q = new LinkedList<Integer>();

or

Queue<Integer> q = new ArrayDeque<Integer>();

ArrayDeque is faster .

like image 65
mainu Avatar answered Feb 26 '26 21:02

mainu


Queue<T> q = new Queue <T> (allParametersGoHere);
like image 21
Solace Avatar answered Feb 26 '26 22:02

Solace