26

I only see a Queue interface, is there no Queue class in the Java Collections?

9 Answers9

37

The Javadocs give a list of classes which implement Queue.

All Known Implementing Classes:

AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedBlockingDeque, LinkedList, PriorityBlockingQueue, PriorityQueue, SynchronousQueue

There are also some subinterfaces which you might find useful:

All Known Subinterfaces:

BlockingDeque<E>, BlockingQueue<E>, Deque<E>

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • 13
    Wonder why people dont go to JavaDocs :P +1 – Perpetualcoder Apr 29 '09 at 20:23
  • 7
    For the really lazy people, I'm going to note that all the ones with DeQue are double-ended queues. – Powerlord Apr 29 '09 at 21:01
  • 2
    I'm confused. I just want a FIFO data structure that lets me enqueue and dequeue items as shown on Wikipedia and none of the specialized stuff. Which one do I pick? Isn't there a Java equivalent to C++'s STL queue? – Pieter Nov 13 '10 at 10:58
  • 4
    @Pieter: Any of these classes will do the basic enqueue/dequeue operations. If you like, you can declare your queue as a Queue like `Queue myQueue = new LinkedList()`, which will force you to stick to the queue operations declared in the interface (like add/remove). – Michael Myers Nov 13 '10 at 18:05
10

Queue has multiple implementations: from the API:

All Known Implementing Classes:

AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, 
DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, LinkedList, 
PriorityBlockingQueue, PriorityQueue, SynchronousQueue

Note that AbstractQueue isn't a concrete class.

Some of these are from the package concurrent, so if you're implementing a jobqueue or something similar, you should go for ConcurrentLinkedQueue or PriorityBlockingQueue (for an heap) for ex.

akappa
  • 10,220
  • 3
  • 39
  • 56
7

The documentation for Queue lists various implementations, including

Pick an implementation that suits your needs.

Rob
  • 47,999
  • 5
  • 74
  • 91
  • According to http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html, ArrayList does not implement Queue. – James McMahon Apr 29 '09 at 20:22
  • Yes, I realised that one when I went back to get a few links; obviously my memory is faulty. – Rob Apr 29 '09 at 20:24
  • 4
    It happens. I'd be impressed if you have the entire Java library memorized. If you had EE memorized I'd back away slowly. – James McMahon Apr 29 '09 at 20:25
  • 2
    Well, there's javax.activation, javax.annotation, javax.ejb, and...wait, where are you going? There's so much more! ;) – Rob Apr 29 '09 at 20:42
4
    Queue<Integer> queue = new ArrayDeque<>();

    queue.add(1);
    queue.add(2);
    queue.add(3);

    while (!queue.isEmpty()) {
        System.out.println(queue.remove());// prints 1 2 3
    }

You can also use a LinkedList. But generally, for a Queue, ArrayDeque is preferred over LinkedList. Because ArrayDeque consumes lesser memory, is faster and doesn't allow nulls. Not allowing null is good, because if you do allow nulls, then when you do a peek() or poll() you could get a null even if the queue is not empty.

developer747
  • 15,419
  • 26
  • 93
  • 147
3

No, there is no Queue class, because there are lots of different ways to implement a queue and you have to pick the one that suits your use case. The same goes for any of the other collections in the collections framework - for example, ArrayList and LinkedList both implement a List. The general pattern, which is a good use of object inheritance, is:

The Interface, e.g. Queue, defines the role you want an object to play;

Sub-interfaces, e.g. Deque, further expands on or specialises the role - in this case a "deque" or double-ended queue allows you to add and remove elements from both ends of the queue, as opposed to only adding to the back and removing from the front;

Classes provide the implementation of how an object carries out the role. For example, an ArrayDeque uses a resizable array to implement a double-ended queue, which has different strengths and weaknesses to LinkedList which uses a linked list.

To elaborate on the idea of an interface as a role, note that even though ArrayDeque implements Deque, you can use it as a Queue without having to worry about that because implementing both interfaces means it can play both roles. Similarly, LinkedList can wear a List, Queue or Deque hat.

For this reason, the normal (recommended) way to use something like the Collections framework is to program to the interface, that is, use an interface when using the class rather than the class name itself. For example, you would instantiate an object like this:

Queue<String> logQueue = new ConcurrentLinkedQueue<String>();
...
logQueue.add("Log message");

In this way you are

  • not tied to a particular class and can use a drop-in replacement if needed without having to modify much code, and
  • are documenting what you are doing with a class by naming the role it plays. The general principle this helps with is self-documenting code, which is essentially to let the code itself be self-explanatory without having to use comments, etc.
Injektilo
  • 581
  • 3
  • 14
3

As well as using the API docs to find "all known implementing classes", there are often other non-public implementation that are nevertheless available through the public API (only without requiring reams of pointless documentation). If you click on "use" you will also find Collections.asLifoQueue (Deque is already a Queue, but it is FIFO rather than a stack).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
2

http://java.sun.com/javase/6/docs/api/java/util/Queue.html -- see section "All Known Implementing Classes". There are a variety of implementations which are suitable for different purposes.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
2

Although the answers sound kind of scornful, They are actually being pretty cool by teaching you how to fish. A Queue is simply a way to look at a collection, so many collections may implement it. As well, things that act like collections but with specific other logic (like thread queues) might use the same interface.

Knowing where to look at the javadocs is a big help. I'm sure you looked but just didn't think to look at the implementations. Live and learn.

Sometimes you may also have to chase down sub-class/extends lists. Like if you looked at Queue and saw AbstractQueue, you might want to see what classes implement that.

I'll get rid of one of your -1s for ya :)

Bill K
  • 62,186
  • 18
  • 105
  • 157
0
import java.util.Queue;

just that

Enqueue function == Queue_Object.add(input_value);

Dequeue function == Queue_Object.pull();   //return the value and delete it from queue
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
FoOzA
  • 1