0% found this document useful (0 votes)
150 views

Java Collections Queue

The Java Queue interface represents an ordered collection of objects that can be inserted at the end and removed from the beginning, like a queue, and provides common implementations like LinkedList and PriorityQueue; it allows adding and accessing elements sequentially, removing the first element, and iterating over all elements; generic Queues were introduced in Java 5 to restrict the types of objects that can be inserted.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

Java Collections Queue

The Java Queue interface represents an ordered collection of objects that can be inserted at the end and removed from the beginning, like a queue, and provides common implementations like LinkedList and PriorityQueue; it allows adding and accessing elements sequentially, removing the first element, and iterating over all elements; generic Queues were introduced in Java 5 to restrict the types of objects that can be inserted.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

t ut orials.jenkov.com http://tutorials.jenkov.com/java-collections/queue.

html
Java Collections - Queue
By Jakob Jenkov
Share on Facebook
Connect with me:

The java.util.Queue interf ace is a subtype of the java.util.Collection interf ace. It represents an ordered list
of objects just like a List, but its intended use is slightly dif f erent. A queue is designed to have elements
inserted at the end of the queue, and elements removed f rom the beginning of the queue. Just like a queue
in a supermarket.
Here is a list of the topics covered in this text:
Queue Implementations
Being a Collection subtype all methods in the Collection interf ace are also available in the Queue
interf ace.
Since Queue is an interf ace you need to instantiate a concrete implementation of the interf ace in order to
use it. You can choose between the f ollowing Queue implementations in the Java Collections API:
java.util.LinkedList
java.util.PriorityQueue
LinkedList is a pretty standard queue implementation.
PriorityQueue stores its elements internally according to their natural order (if they implement Comparable
), or according to a Comparator passed to the PriorityQueue.
There are also Queue implementations in the java.util.concurrent package, but I will leave the concurrency
utilities out of this tutorial.
Here are a f ew examples of how to create a Queue instance:
Queue queueA = new LinkedList();
Queue queueB = new PriorityQueue();
Adding and Accessing Elements
To add elements to a Queue you call its add() method. This method is inherited f rom the Collection
interf ace. Here are a f ew examples:
Queue queueA = new LinkedList();
queueA.add("element 1");
queueA.add("element 2");
queueA.add("element 3");
The order in which the elements added to the Queue are stored internally, depends on the implementation.
The same is true f or the order in which elements are retrieved f rom the queue. You should consult the
JavaDoc's f or more inf ormation about the specif ic Queue implementations.
You can peek at the element at the head of the queue without taking the element out of the queue. This is
done via the element() method. Here is how that looks:
Object rstElement = queueA.element();
To take the f irst element out of the queue, you use the remove() method which is described later.
You can also iterate all elements of a queue, instead of just processing one at a time. Here is how that
looks:
Queue queueA = new LinkedList();
queueA.add("element 0");
queueA.add("element 1");
queueA.add("element 2");
//access via Iterator
Iterator iterator = queueA.iterator();
while(iterator.hasNext(){
String element = (String) iterator.next();
}
//access via new for-loop
for(Object object : queueA) {
String element = (String) object;
}
When iterating the queue via its Iterator or via the f or-loop (which also uses the Iterator behind the scene,
the sequence in which the elements are iterated depends on the queue implementation.
Removing Elements
To remove elements f rom a queue, you call the remove() method. This method removes the element at the
head of the queue. In most Queue implementations the head and tail of the queue are at opposite ends. It
is possible, however, to implement the Queue interf ace so that the head and tail of the queue is in the
same end. In that case you would have a stack.
Here is a remove example();
Object rstElement = queueA.remove();
Generic Queue
By def ault you can put any Object into a Queue, but f rom Java 5, Java Generics makes it possible to limit
the types of object you can insert into a Queue. Here is an example:
Queue<MyObject> queue = new LinkedList<MyObject>();
This Queue can now only have MyObject instances inserted into it. You can then access and iterate its
elements without casting them. Here is how it looks:
MyObject myObject = queue.remove();
for(MyObject anObject : queue){
//do someting to anObject...
}
For more inf ormation about Java Generics, see the Java Generics Tutorial.
More Details in the JavaDoc
There is a lot more you can do with a Queue, but you will have to check out the JavaDoc f or more details.
This text f ocused on the two most common operations: Adding / removing elements, and iterating the
elements.
Next: Java Collections - Deque

You might also like