Initializing a Queue in Java

10,694

Solution 1

Queue<T> q = new Queue <T> (allParametersGoHere);

Solution 2

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 .

Share:
10,694
jnel899
Author by

jnel899

Updated on June 26, 2022

Comments

  • jnel899
    jnel899 almost 2 years

    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?

  • Solace
    Solace about 10 years
    "I have a class Queue that fully implements the Queue interface"
  • jnel899
    jnel899 about 10 years
    Thanks for both answers!