Java Queue Interface, Implementations and Example
A Queue is a collection used to hold multiple elements prior to processing. Besides basic Collection operations, queue interface provides additional insertion, removal, and examine operations as mentioned below.
Queue Interface Java SE 8
public interface Queue<E> extends Collection<E> { //Insert operations boolean add(E e) boolean offer(E e); //Remove operations E remove(); E poll(); //Examine operations E peek(); E element(); }
Queues typically, but not necessarily, order elements in a FIFO (first-in-first-out) manner.
Each Queue interface method exists in two forms
- Methods that throws exception, if the operation fails – add(e), remove(), element().
- Methods that returns a special value, if the operation fails (either null or false) – offer(e), poll(), peek().
Difference between element() and peek()
You might expect this interview question on the the difference between element() and peek() methods of Queue interface ?
The answer is: the commonality is both retrieves head element of the queue, but the element() differs from peek() only in that it throws an exception if queue is empty and peek() returns null in this case.
offer(e) Vs add(e)
The method offer(e) inserts the specified element in to the queue if it is possible to do without violating capacity restrictions. When you use capacity-restricted queue in your implementation offer(e) method is preferred over add(e) method, which can fail to insert an element and just throws an exception.
Example: Queue Interface
package com.sneppets.corejava.collections; import java.util.LinkedList; import java.util.Queue; /** * Program to demonstrate Queue Interface Methods * @author sneppets.com */ public class QueueInterfaceExample { public static void main(String[] args) { Queue<String> queue = new LinkedList<String>(); //insert operation queue.add("AB"); queue.add("BC"); queue.add("CD"); queue.add("DE"); //display queue System.out.println("Queue elements :" + queue); //remove operation System.out.println("Removed element from queue (head): " + queue.remove()); //examine operation System.out.println("Retrieved element from queue (head): " + queue.peek()); } }
Output
Queue elements :[AB, BC, CD, DE] Removed element from queue (head): AB Retrieved element from queue (head): BC
Implementations of “Queue” Interface
The following are the general-purpose implementations of Queue interface
- LinkedList: It implements the Queue interface, providing FIFO queue operations for add, poll, and so on.
- PriorityQueue: It is a priority queue based on the heap data structure. This queue orders elements according to an order specified at construction time, which can be the natural ordering of the elements or the ordering imposed by an explicit Comparator.
The following are the Concurrent Queue implementations classes
The java.util.concurrent package contains a set of synchronized Queue interfaces and classes. BlockingQueue extends Queue with operations that wait for the queue to become nonempty when retrieving an element and for space to become available in the queue when storing an element. This interface is implemented by the following classes:
- ArrayBlockingQueue: A bounded FIFO blocking queue backed by an array.
- DelayQueue: A scheduling queue based on time backed by a heap
- LinkedBlockingQueue: An optionally-bounded FIFO blocking queue backed by linked nodes.
- LinkedBlockingDeque: An optionally-bounded blocking deque based on linked nodes.
- LinkedTransferQueue: An unbounded TransferQueue based on linked nodes.
- PriorityBlockingQueue: An unbounded blocking priority queue backed by a heap.
- SynchronousQueue: A blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa.
Related Posts
- List Interface, Implementations and Examples
- Set Interface, Implementations and Examples
- Collection Interface and Examples
- EnumSet Example and Efficiency
- Collection(s) and Collections Framework