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 the 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.
Queue Collection in Java

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.
  • 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.


boolean offer(object): used to insert the specified element into this queue.


Object remove(): used to retrieves and removes the head of this queue.


Object poll(): used to retrieves and removes the head of this queue, or returns null if this queue is empty.


Object element(): used to retrieves, but does not remove, the head of this queue.


Object peek(): used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Example :

Leave a Reply

Contact Us
close slider
Contact Us

How can we help you with?