Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 06 Apr 2016
PriorityQueue extendsAbstractQueue and implements the Queueinterface. It creates a queue that is prioritized based on the comparator.

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. PriorityQueue is declared as follows:
class PriorityQueue< E >
where E specifies the type of the object that is stored in a queue. PriorityQueue has six constructors which are defined below:
ConstructorDescription
PriorityQueue()Builds an empty queue.
PriorityQueue(int capacity)Builds a queue that has the specified initial capacity.
PriorityQueue(int capacity, Comparator< ?super E > comp)Builds a queue that has the specified initial capacity and comparator.
PriorityQueue(Collection< ? extends E > c)Builds a queue that is initialized with the elements of c.
PriorityQueue(PriorityQueue< ? extends E > c)Builds a queue that is initialized with the elements of c.
PriorityQueue(SortedSet< ? extends E > c)Builds a queue that is initialized with the elements of c.
If no comparator is specified, then the default comparator will order the type of data in the queue in ascending order. By providing a custom comparator, you can specify a different ordering scheme. The comparator() method is declared as follows:
Comparator< ? super E > comparator()
To add or remove elements to the PriorityQueue, the methods defined by Queue interface must be implemented:
MethodDescription
E element()Returns the element at the head of the queue without removing the element. Throws Exception if the queue is empty.
boolean offer(E obj)Adds obj to the queue. Returns true if objwas added else returnsfalse.
E peek()Returns the element at the head of the queue without removing the element. Returns null if queue is empty.
E poll()Returns the element at the head of the queue, removing the element. Returns null if queue is empty.
E remove()Removes the element at the head of the queue, returning the element in process. Throws an exception if queue is empty.
The following example demonstrates these methods:
PriorityQueueDemo
import java.util.*;

class PriorityQueueDemo
{
    public static void main(String arg[])
    {
        PriorityQueue<Integer> num = new PriorityQueue<Integer>(); // LINE A
        num.offer(1);
        num.offer(5);
        num.offer(2);
        //num.offer(null); LINE B
        for (int i = 0; i < 3; i++)
        {
            System.out.println(num.peek());
            num.remove();
        }
    
    }
}
OUTPUT

1
2
5

DESCRIPTION

In this example, a PriorityQueue is declared which is of type Integer.The offer() adds elements to the PriorityQueue. Since no comparator is specified,the default comparator is invoked which stores the elements in ascending order. So the elements are stored in this order: 1, 2, 5. The peek() returns the element at the head of the queue without removing the element. The remove() removes the element at the head of the queue.

THINGS TO TRY
  • At LINE A replace the code with the below code.
    PriorityQueue<Integer> num = new PriorityQueue<>(1, new Comparator<Integer>()
    {

        public int compare(Integer i, Integer j)
        {
            return j - i;
        }
    });
    The above code changes the natural ordering of the PriorityQueue to descending order so the output will be
    5
    2
    1
  • Uncomment LINE B in the above program to see java.lang.NullPointerException since priority queue does not permit null elements.

Dependent Topics : Java Iterator 

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.