164

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?

wam090
  • 2,823
  • 8
  • 33
  • 36

8 Answers8

169

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 17
    From [ArrayDeque](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html): "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. – Vadzim Sep 30 '16 at 18:13
165

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) {
     ...
   };
   ...
};
kimbaudi
  • 13,655
  • 9
  • 62
  • 74
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • 23
    Oh dear... I fear someone reading this will use an anonymous `Queue`... but +1 anyways. – Tom Jan 07 '11 at 23:05
  • 1
    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) – Tom Jan 07 '11 at 23:08
  • 1
    @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. – Edwin Buck Jan 10 '11 at 04:51
  • 1
    Why not mention `ArrayDeque` – JW.ZG Dec 11 '16 at 22:59
  • 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. – Sreekanth Karumanaghat Jul 21 '20 at 09:33
43
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

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    `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. – Mihai Toader Jan 07 '11 at 15:05
  • @Tod yes was on the way .. :) – jmj Jan 07 '11 at 15:06
  • Thanks @JigarJoshi!! is there any way to do same thing with stack? I couldn't find anything. – Zehra Subaş Jun 07 '18 at 23:28
  • @Zeh `Stack stack = new Stack<>(); stack.push("a"); stack.push("b"); System.out.println(stack.pop());` `import java.util.Stack;` – jmj Jun 08 '18 at 07:30
16

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.

istrupin
  • 1,423
  • 16
  • 32
zmf
  • 9,095
  • 2
  • 26
  • 28
14

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
devDeejay
  • 5,494
  • 2
  • 27
  • 38
8

Queue in Java is defined as an interface and many ready-to-use implementation is present as part of JDK release. Here are some: LinkedList, Priority Queue, ArrayBlockingQueue, ConcurrentLinkedQueue, Linked Transfer Queue, Synchronous Queue etc.

SO You can create any of these class and hold it as Queue reference. for example

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {

 public static void main (String[] args) {
  Queue que = new LinkedList();
  que.add("first");
  que.offer("second");
  que.offer("third");
  System.out.println("Queue Print:: " + que);
  
  String head = que.element();
  System.out.println("Head element:: " + head);
  
  String element1 = que.poll();
  System.out.println("Removed Element:: " + element1);
  
  System.out.println("Queue Print after poll:: " + que);
  String element2 = que.remove();
  System.out.println("Removed Element:: " + element2);
  
  System.out.println("Queue Print after remove:: " + que);  
 }
}

You can also implement your own custom Queue implementing Queue interface.

drac_o
  • 427
  • 5
  • 11
Java Guru
  • 466
  • 4
  • 12
7

Queue is an interface in java, you can not do that.

Instead you have two options:

option1:

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

option2:

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

I recommend using option2 as it is bit faster than the other

Sujit
  • 101
  • 1
  • 3
4

Queue is an interface in java, you could not do that. try:

Queue<Integer> Q = new LinkedList<Integer>();
roottraveller
  • 7,942
  • 7
  • 60
  • 65
lcl
  • 1,045
  • 11
  • 13