How do I instantiate a Queue object in java?

JavaData StructuresQueue

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 2 - Java

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 3 - Java

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 - Java

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 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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.

Solution 8 - Java

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwam090View Question on Stackoverflow
Solution 1 - JavaJon SkeetView Answer on Stackoverflow
Solution 2 - JavaEdwin BuckView Answer on Stackoverflow
Solution 3 - JavajmjView Answer on Stackoverflow
Solution 4 - JavazmfView Answer on Stackoverflow
Solution 5 - JavadevDeejayView Answer on Stackoverflow
Solution 6 - JavaSujitView Answer on Stackoverflow
Solution 7 - JavaJava GuruView Answer on Stackoverflow
Solution 8 - JavalclView Answer on Stackoverflow