Queue in Java – Free Java Tutorials and Online Training

Queue is the child interface of Collection Interface.

Queue has mainly two types:

1) Priority Queue
2) Blocking Queue.


Blocking Queue has two types
1) Priority Blocking Queue
2) Linked Blocking Queue.

If we want to represent a group of individual objects prior to processing then we should go for queue.

For example, before sending SMS message, all mobile numbers we have to store in some data structure. In which order we have added mobile numbers, in the same order only message should be sent. For this FIFO (First In First Out) requirement, queue is the best choice. Usually, queue follows FIFO order but based on our requirement we can implement our own priority order also (PriorityQueue).

Note : From  1.5 version onwards, LinkedList class also implements Queue interface.

Note : LinkedList based implementation of queue always follows FIFO order.


Queue interface specific methods

  • boolean offer(Object obj) – This method is used to add an object to the queue.
  • Object peek() – This method returns the head element of a queue. If the queue is empty then this method returns ‘null’.
  • Object element() – This method also returns the head element of the queue. If the queue is empty, then this method will raise NoSuchElementException.
  • Object poll() – This method is used to remove and return head element of a queue. If the queue is empty then this method returns ‘null’.
  • Object remove() – This method is used to remove and returns the head element from the queue. If the queue is empty then this method will raise NoSuchElementException.

Priority Queue

If we want to represent a group of individual objects prior to processing according to some priority, then we should use PriorityQueue. The Priority can be either default natural sorting order or customized sorting order defined by the comparator. PriorityQueue has the following features:

  • Insertion order is not preserved and it is based on some priority.
  • Duplicate objects are not allowed.
  • If we are depending on default natural sorting order, compulsory the object should be homogeneous and comparable. Otherwise, we will get runtime exception saying ClassCastException.
  • If we are defining our own sorting by comparator then objects need not be homogeneous and comparable.
  • ‘null’ is not allowed even as the first element also.

Constructors in Queue

1) PriorityQueue q = new PriorityQueue()

By this constructor, a new PriorityQueue will be created with the initial capacity 11 and sorting technique is default natural sorting order.

2) PriorityQueue q = new PriorityQueue(int Initial_Capacity)

By this constructor, a new PriorityQueue will be created with the initial capacity specified by Initial_Capacity and sorting technique is default natural sorting order.

3) PriorityQueue q = new PriorityQueue(int Initial_Capacity, Comparator  c)

By this constructor, a new PriorityQueue will be created with the initial capacity specified by Initial_Capacity and sorting technique is specified by comparator object c.

4) PriorityQueue q = new PriorityQueue(SortedSet s)

This constructor is used for inter conversion purpose of SortedSet objects to PriorityQueue objects.

5) PriorityQueue q = new PriorityQueue(Collection c)

This constructor is used for inter conversion purpose of Collection objects to PriorityQueue objects.

Let us take a sample program of PriorityQueue:

import java.util.*;
class PriorDemo
{
    public static void main(String[] args)
    {
        PriorityQueue p = new PriorityQueue();

// A new PriorityQueue object will be created with initial capacity 11 
// and sorting technique as default natural sorting order
 
        System.out.println(p.peek());    // Output is null as queue is empty
        for(int i=0;i<11;i++)
            p.offer(i);

// 11 elements inserted in the queue

        System.out.println(p);

// Entire queue will be printed

    }
}

When we run the code we will get the following output:

null
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Note: Some platforms won’t provide proper support for PriorityQueues.

I hope this tutorial will be helpful for you , So this is all about the Different types of Queue in Java and covers all interview questions of queue.

Leave a Comment

Your email address will not be published. Required fields are marked *