Understanding Queue collection in Java with real life example

In this Queue Collection tutorial, we are going to learn what queue interface offers and how can we get best out of it using LinkedList and PriorityQueue concrete classes.

The principle of Java Queue is same as the queue we follow in real life. Suppose you are Lucas, standing in the middle of the queue to collect the boarding pass of your flight, Bob is standing before you and Alice standing after you. In other words, Bob entered in the queue before you and Alice entered after you.

Now, Bob is the head of the queue and Alice is the tail of the queue. As queue follow the FIFO (first-in-first-out) rule Bob will get the boarding pass first then Lucas and Alice will get the pass at the end.

Facts about Queue Collection:

  • A Java Queue is a collection that holds elements before performing any operation.
  • Java Queue has two ends known as head and tail.
  • In the simple queue, objects are added to the tail and removed from the head and the object added first will be removed first. The principle used in Java Queue is similar to queue on ticket counter, that is, FIFO (First In First Out), but that is not true for every concrete class
  • Apart from the collection, Queue provides operations, such as insertion, extraction, inspection.
  • Each queue shows the following nature:
    • throws Exception when an operation fails
    • the other returns a special value (either null or false, depending on the operation)
 Action Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()

PriorityQueue Concrete Class

  • Introduced with Java 5, implements the Queue interface.
  • Arranged in natural order or Comparator interface(custom).The PriorityQueue class provides a Queue implementing the concept of priority in and priority-out instead of FIFO.

LinkedList Concrete Class

  •  A linked list is an ordered set of data elements, each containing a link to its successor. Here we need to apply the application of linked list to perform basic operations of a queue.Application of Queue using LinkedList is shown with the example given below in the link.

QueueUsingLinkedList

  •  LinkedList implementation of Queue is dynamic in nature.
Concrete Class/Map Interface Duplicate Elements Ordered Sorted Methods Invoked on Elements Allow Null?
LinkedList List, Queue Yes By Index No equals() Yes
PriorityQueue Queue Yes Natural Ordering Natural Ordering equals() and hashCode() No

Java Collections Framework supports the following types of queues.

  • A simple queue allows insertion at the tail and removal from the head.
  • A priority queue assigns a priority to each element and allows the element with the highest priority to be removed next from the queue.
  • A delay queue adds a delay to each element and removes the element only when its delay has elapsed.
  • A doubly ended queue allows for insertion and removal of its elements from the head and the tail.
  • A blocking queue blocks the thread that adds elements to it when it is full and it blocks the thread removing elements from it when it is empty.
  • A transfer queue is a blocking queue where a handoff of an object occurs between the producer thread and a consumer thread.
  • A blocking doubly ended queue is a combination of a doubly ended queue and a blocking queue.

Methods of Java Queue Interface

  • boolean add(object):  used to insert the specified element into this queue and return true upon success.

Example:

Queue queueA = new LinkedList();

queueA.add("element 1");
queueA.add("element 2");
queueA.add("element 3");
  • boolean offer(object): used to insert the specified element into this queue.

Example:

Queue<String> queueCustomers = new LinkedList<>();

queueCustomers.offer("Jack");

System.out.println("next: " + queueCustomers.poll());

System.out.println("next: " + queueCustomers.poll());   // returns null
  • Object remove(): used to retrieves and removes the head of this queue.

Example:

Object firstElement = queueA.remove();
  • Object poll(): used to retrieves and removes the head of this queue, or returns null if this queue is empty.

Example:

Queue<String> queueCustomers = new LinkedList<>();

queueCustomers.offer("Jack");

System.out.println("next: " + queueCustomers.poll());

System.out.println("next: " + queueCustomers.poll());   // returns null
  • Object element(): used to retrieves, but does not remove, the head of this queue.

Example:

Queue<String> queueCustomers = new PriorityQueue<>();

queueCustomers.offer("Jack");

System.out.println("who's next: " + queueCustomers.poll());

// this returns null in case the queue is empty

System.out.println("who's next: " + queueCustomers.peek());

// this throws exception in case the queue is empty

System.out.println("who's next: " + queueCustomers.element());
  • Object peek(): used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Example :

int head = q.peek();

    System.out.println("head of queue-" + head);

Find all the working examples of Java Collections Framework on the codedbug GitHub Repo