Queue Data Structure

Queue data structure is a part of java collection framework. It is available as an interface under java.util package. Queues are generally based on First in first out (FIFO) principle which means the element inserted first will be removed first and element inserted last will be removed last. This unique property of queue makes it useful to store number of elements before processing them.

Queue is an interface in java which is implemented by many classes like PriorityQueue, Deque, LinkedList. Not all implementation classes follow the first in fist out ordering but follows some or the other kind of ordering. For example; Priority queue orders the element according to their priorities.

Representation of Queue data structure

The first element in a queue is called the head. All the elements added after head are inserted at the tail of the queue. Queues can be thought of having one open end for insertion and other end for removal of elements.

Step by step insertion and deletion in a queue

As Queue extends java collection framework, it supports all the collection framework functions including add, remove, element. Apart from these Queue also provides additional functions for insertion, extraction, and inspection. This means that these functions are present in two forms in Queue where one form throws exception if the operation fails (this is default behaviour of collection framework inherited methods) and the other form added in Queue interface which returns null if the operation fails.

OperationThrows ExceptionReturns value(true/false/null)
Insertadd(e)offer(e)
Removeremove(e)poll()
Examineelement(e)peek()

Add Vs Offer() – offer method is used to insert an element in the queue if possible. This method returns true if element is successfully added and returns false if it fails to add an element. On the contrary, the add method which is inherited from collection framework throws an exception in case it fails to add an element to the queue. It is must to have an exceptional handling mechanism while calling add method. But not while calling offer method.

remove() Vs poll() – remove and poll methods are used to remove and return the head of the queue. In case the queue is empty poll methods throws exception but remove returns null.

Element and peek() – element and peek method are used to get the value of head element. It does not remove the head of the queue. Incase of empty queue , peek method throws an exception whereas element method returns null.

Example of Queue data structure

Linked List is one implementation of Queue.

import java.util.Queue;

public class QueueExample {
  
  public static void main(String[] args) {

    Queue<String> queue = new java.util.LinkedList<String>();

    // Add elements to the queue
    queue.add("Rose");
    queue.add("Mary");
    queue.add("Jasmine");

    System.out.println("original queue : " + queue);

    // check head element of the queue
    String head = queue.peek();
    System.out.println("head of the queue :" + head);

    // remove element from the queue
    queue.poll();
    System.out.println("queue after removing an element: " + queue);
  }
}            
            

OUTPUT

original queue : [Rose, Mary, Jasmine]
head of the queue :Rose
queue after removing an element: [Mary, Jasmine]            
          

Summary

In this article we learnt about the Queue Data Structure using Java. For better understanding we looked into some code snippets of queues add, peek & poll operations.
Hope you liked the article !


Leave a Comment