How do I instantiate a Queue object in java?

353,050

Solution 1

A Queue is an interface, which means you cannot construct a Queue directly.

The best option is to construct off a class that already implements the Queue interface, like one of the following: AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, or SynchronousQueue.

An alternative is to write your own class which implements the necessary Queue interface. It is not needed except in those rare cases where you wish to do something special while providing the rest of your program with a Queue.

public class MyQueue<T extends Tree> implements Queue<T> {
   public T element() {
     ... your code to return an element goes here ...
   }

   public boolean offer(T element) {
     ... your code to accept a submission offer goes here ...
   }

   ... etc ...
}

An even less used alternative is to construct an anonymous class that implements Queue. You probably don't want to do this, but it's listed as an option for the sake of covering all the bases.

new Queue<Tree>() {
   public Tree element() {
     ...
   };

   public boolean offer(Tree element) {
     ...
   };
   ...
};

Solution 2

Queue is an interface. You can't instantiate an interface directly except via an anonymous inner class. Typically this isn't what you want to do for a collection. Instead, choose an existing implementation. For example:

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

or

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

Typically you pick a collection implementation by the performance and concurrency characteristics you're interested in.

Solution 3

Queue<String> qe=new LinkedList<String>();

qe.add("b");
qe.add("a");
qe.add("c");

Since Queue is an interface, you can't create an instance of it as you illustrated

Solution 4

Queue is an interface; you can't explicitly construct a Queue. You'll have to instantiate one of its implementing classes. Something like:

Queue linkedList = new LinkedList();

Here's a link to the Java tutorial on this subject.

Solution 5

enter image description here

The Queue interface extends java.util.Collection with additional insertion, extraction, and inspection operations like:

+offer(element: E): boolean // Inserting an element

+poll(): E // Retrieves the element and returns NULL if queue is empty

+remove(): E // Retrieves and removes the element and throws an Exception if queue is empty

+peek(): E // Retrieves,but does not remove, the head of this queue, returning null if this queue is empty.

+element(): E // Retrieves, but does not remove, the head of this queue, throws an exception if te queue is empty.

Example Code for implementing Queue:

java.util.Queue<String> queue = new LinkedList<>();
queue.offer("Hello");
queue.offer("StackOverFlow");
queue.offer("User");

System.out.println(queue.peek());

while (queue.size() > 0){
    System.out.println(queue.remove() + " ");
}
//Since Queue is empty now so this will return NULL
System.out.println(queue.peek());

Output Of the code :

Hello
Hello 
StackOverFlow 
User 
null
Share:
353,050

Related videos on Youtube

wam090
Author by

wam090

C# Developer Java is my bread &amp; butter Having interest in IOS and OSX platforms... Experience in arduino development Databases here &amp; there

Updated on October 30, 2021

Comments

  • wam090
    wam090 over 2 years

    When I try:

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

    The compiler is giving me an error. Any help?

    Also, if I want to initialize a queue do I have to implement the methods of the queue?

  • Mihai Toader
    Mihai Toader over 13 years
    java.util.Queue is an interface. You can't instantiate interfaces. You need to create an instance of a class implementing that interface. In this case a LinkedList is such a class.
  • Tom
    Tom over 13 years
    Oh dear... I fear someone reading this will use an anonymous Queue... but +1 anyways.
  • Tom
    Tom over 13 years
    Actually, Jon's is more clear. I will +1 this if you update it to mention concurrency and get rid of the code for anonymous classes... I think it makes the answer more confusing for someone that wants to know what to do because they almost surely don't want to do that. (Even if they wanted their own class, there is no need to make it anonymous)
  • Edwin Buck
    Edwin Buck over 13 years
    @Tom didn't take out the anonymous class info as it's good to know it is possible, but I put in the "Write your own implementation" before it which distances it further from the first listed (more common) alternatives.
  • Md Faisal
    Md Faisal over 7 years
    this is not working anymore..! Although this declaration is working --> Queue<Character> queue=new ArrayDeque<Character>();
  • zmf
    zmf over 7 years
    @MdFaisal Works fine for me w/ java version "1.7.0_71"
  • Vadzim
    Vadzim over 7 years
    From ArrayDeque: "This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue". It's due to CPU-cache-friendly data locality and less frequent allocations.
  • JW.ZG
    JW.ZG over 7 years
    Why not mention ArrayDeque
  • Zehra Subaş
    Zehra Subaş almost 6 years
    Thanks @JigarJoshi!! is there any way to do same thing with stack? I couldn't find anything.
  • jmj
    jmj almost 6 years
    @Zeh Stack<String> stack = new Stack<>(); stack.push("a"); stack.push("b"); System.out.println(stack.pop()); import java.util.Stack;
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat over 3 years
    I am not able to find enqueue() method in any of the classes you mentioned, I am only able to find add() method, please correct me if I'm wrong.

Related