We know that `Queue`

follows **F**irst-**I**n-**F**irst-**O**ut model but sometimes we need to process the objects in the queue based on the priority. That is when Java`PriorityQueue`

is used.

For example, let’s say we have an application that generates stocks reports for daily trading session. This application processes a lot of data and takes time to process it. So customers are sending request to the application that is actually getting queued but we want to process premium customers first and standard customers after them. So in this case **PriorityQueue** implementation in java can be really helpful.

PriorityQueue is an unbounded queue based on a priority heap and the elements of the priority queue are ordered by default in natural order. We can provide a **Comparator** for ordering at the time of instantiation of priority queue.

Java Priority Queue doesn’t allow `null`

values and we can’t create PriorityQueue of Objects that are non-comparable. We use java Comparable and Comparator for sorting Objects and Priority Queue use them for priority processing of it’s elements.

The simplest way to implement a priority queue data type is to keep an associative array mapping each priority to a list of elements with that priority. If association lists or hash tables are used to implement the associative array, adding an element takes constant time but removing or peeking at the element of highest priority takes linear (O(n)) time, because we must search all keys for the largest one. If a self-balancing binary search tree is used, all three operations take O(log n) time; this is a popular solution in environments that already provide balanced trees but nothing more sophisticated.

There are a number of specialized heap data structures that either supply additional operations or outperform the above approaches. The binary heap uses O(log n) time for both operations, but allows peeking at the element of highest priority without removing it in constant time. Binomial heaps add several more operations, but require O(log n) time for peeking. Fibonacci heaps can insert elements, peek at the maximum priority element, and decrease an element’s priority in amortized constant time (deletions are still O(log n)).

// BinaryHeap class

//

// CONSTRUCTION: empty or with initial array.

//

// ******************PUBLIC OPERATIONS*********************

// void insert( x ) –> Insert x

// Comparable deleteMin( )–> Return and remove smallest item

// Comparable findMin( ) –> Return smallest item

// boolean isEmpty( ) –> Return true if empty; else false

// void makeEmpty( ) –> Remove all items

// ******************ERRORS********************************

// Throws UnderflowException for findMin and deleteMin when empty

/**

* Implements a binary heap.

* Note that all “matching” is based on the compareTo method.

* @author Bhavesh Gadoya

*/

public class BinaryHeap implements PriorityQueue {

/**

* Construct the binary heap.

*/

public BinaryHeap( ) {

currentSize = 0;

array = new Comparable[ DEFAULT_CAPACITY + 1 ];

}

/**

* Construct the binary heap from an array.

* @param items the inital items in the binary heap.

*/

public BinaryHeap( Comparable [ ] items ) {

currentSize = items.length;

array = new Comparable[ items.length + 1 ];

for( int i = 0; i < items.length; i++ )

array[ i + 1 ] = items[ i ];

buildHeap( );

}

/**

* Insert into the priority queue.

* Duplicates are allowed.

* @param x the item to insert.

* @return null, signifying that decreaseKey cannot be used.

*/

public PriorityQueue.Position insert( Comparable x ) {

if( currentSize + 1 == array.length )

doubleArray( );

// Percolate up

int hole = ++currentSize;

array[ 0 ] = x;

for( ; x.compareTo( array[ hole / 2 ] ) < 0; hole /= 2 )

array[ hole ] = array[ hole / 2 ];

array[ hole ] = x;

return null;

}

/**

* @throws UnsupportedOperationException because no Positions are returned

* by the insert method for BinaryHeap.

*/

public void decreaseKey( PriorityQueue.Position p, Comparable newVal ) {

throw new UnsupportedOperationException( “Cannot use decreaseKey for binary heap” );

}

/**

* Find the smallest item in the priority queue.

* @return the smallest item.

* @throws UnderflowException if empty.

*/

public Comparable findMin( ) {

if( isEmpty( ) )

throw new UnderflowException( “Empty binary heap” );

return array[ 1 ];

}

/**

* Remove the smallest item from the priority queue.

* @return the smallest item.

* @throws UnderflowException if empty.

*/

public Comparable deleteMin( ) {

Comparable minItem = findMin( );

array[ 1 ] = array[ currentSize– ];

percolateDown( 1 );

return minItem;

}

/**

* Establish heap order property from an arbitrary

* arrangement of items. Runs in linear time.

*/

private void buildHeap( ) {

for( int i = currentSize / 2; i > 0; i– )

percolateDown( i );

}

/**

* Test if the priority queue is logically empty.

* @return true if empty, false otherwise.

*/

public boolean isEmpty( ) {

return currentSize == 0;

}

/**

* Returns size.

* @return current size.

*/

public int size( ) {

return currentSize;

}

/**

* Make the priority queue logically empty.

*/

public void makeEmpty( ) {

currentSize = 0;

}

private static final int DEFAULT_CAPACITY = 100;

private int currentSize; // Number of elements in heap

private Comparable [ ] array; // The heap array

/**

* Internal method to percolate down in the heap.

* @param hole the index at which the percolate begins.

*/

private void percolateDown( int hole ) {

int child;

Comparable tmp = array[ hole ];

for( ; hole * 2 <= currentSize; hole = child ) {

child = hole * 2;

if( child != currentSize &&

array[ child + 1 ].compareTo( array[ child ] ) < 0 )

child++;

if( array[ child ].compareTo( tmp ) < 0 )

array[ hole ] = array[ child ];

else

break;

}

array[ hole ] = tmp;

}

/**

* Internal method to extend array.

*/

private void doubleArray( ) {

Comparable [ ] newArray;

newArray = new Comparable[ array.length * 2 ];

for( int i = 0; i < array.length; i++ )

newArray[ i ] = array[ i ];

array = newArray;

}

// Test program

public static void main( String [ ] args ) {

int numItems = 10000;

BinaryHeap h1 = new BinaryHeap( );

Integer [ ] items = new Integer[ numItems – 1 ];

int i = 37;

int j;

for( i = 37, j = 0; i != 0; i = ( i + 37 ) % numItems, j++ ) {

h1.insert( new Integer( i ) );

items[ j ] = new Integer( i );

}

for( i = 1; i < numItems; i++ )

if( ((Integer)( h1.deleteMin( ) )).intValue( ) != i )

System.out.println( “Oops! ” + i );

BinaryHeap h2 = new BinaryHeap( items );

for( i = 1; i < numItems; i++ )

if( ((Integer)( h2.deleteMin( ) )).intValue( ) != i )

System.out.println( “Oops! ” + i );

}

}

// PriorityQueue interface

//

// ******************PUBLIC OPERATIONS*********************

// Position insert( x ) –> Insert x

// Comparable deleteMin( )–> Return and remove smallest item

// Comparable findMin( ) –> Return smallest item

// boolean isEmpty( ) –> Return true if empty; else false

// void makeEmpty( ) –> Remove all items

// int size( ) –> Return size

// void decreaseKey( p, v)–> Decrease value in p to v

// ******************ERRORS********************************

// Throws UnderflowException for findMin and deleteMin when empty

/**

* PriorityQueue interface.

* Some priority queues may support a decreaseKey operation,

* but this is considered an advanced operation. If so,

* a Position is returned by insert.

* Note that all “matching” is based on the compareTo method.

* @author Bhavesh Gadoya

*/

public interface PriorityQueue {

/**

* The Position interface represents a type that can

* be used for the decreaseKey operation.

*/

public interface Position {

/**

* Returns the value stored at this position.

* @return the value stored at this position.

*/

Comparable getValue( );

}

/**

* Insert into the priority queue, maintaining heap order.

* Duplicates are allowed.

* @param x the item to insert.

* @return may return a Position useful for decreaseKey.

*/

Position insert( Comparable x );

/**

* Find the smallest item in the priority queue.

* @return the smallest item.

* @throws UnderflowException if empty.

*/

Comparable findMin( );

/**

* Remove the smallest item from the priority queue.

* @return the smallest item.

* @throws UnderflowException if empty.

*/

Comparable deleteMin( );

/**

* Test if the priority queue is logically empty.

* @return true if empty, false otherwise.

*/

boolean isEmpty( );

/**

* Make the priority queue logically empty.

*/

void makeEmpty( );

/**

* Returns the size.

* @return current size.

*/

int size( );

/**

* Change the value of the item stored in the pairing heap.

* This is considered an advanced operation and might not

* be supported by all priority queues. A priority queue

* will signal its intention to not support decreaseKey by

* having insert return null consistently.

* @param p any non-null Position returned by insert.

* @param newVal the new value, which must be smaller

* than the currently stored value.

* @throws IllegalArgumentException if p invalid.

* @throws UnsupportedOperationException if appropriate.

*/

void decreaseKey( Position p, Comparable newVal );

}

/**

* Exception class for access in empty containers

* such as stacks, queues, and priority queues.

* @author Bhavesh Gadoya

*/

public class UnderflowException extends RuntimeException {

/**

* Construct this exception object.

* @param message the error message.

*/

public UnderflowException( String message ) {

super( message );

}

}

In-built java implementation of priorityQueue :

package com.journaldev.collections;
public class Customer {
private int id;
private String name;
public Customer(int i, String n){
this.id=i;
this.name=n;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}

We will use java random number generation to generate random customer objects. For natural ordering, I will use Integer that is also a java wrapper class.

Here is our final test code that shows how to use priority queue in java.

package com.journaldev.collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class PriorityQueueExample {
public static void main(String[] args) {
//natural ordering example of priority queue
Queue<Integer> integerPriorityQueue = new PriorityQueue<>(7);
Random rand = new Random();
for(int i=0;i<7;i++){
integerPriorityQueue.add(new Integer(rand.nextInt(100)));
}
for(int i=0;i<7;i++){
Integer in = integerPriorityQueue.poll();
System.out.println("Processing Integer:"+in);
}
//PriorityQueue example with Comparator
Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
addDataToQueue(customerPriorityQueue);
pollDataFromQueue(customerPriorityQueue);
}
//Comparator anonymous class implementation
public static Comparator<Customer> idComparator = new Comparator<Customer>(){
@Override
public int compare(Customer c1, Customer c2) {
return (int) (c1.getId() - c2.getId());
}
};
//utility method to add random data to Queue
private static void addDataToQueue(Queue<Customer> customerPriorityQueue) {
Random rand = new Random();
for(int i=0; i<7; i++){
int id = rand.nextInt(100);
customerPriorityQueue.add(new Customer(id, "Pankaj "+id));
}
}
//utility method to poll data from queue
private static void pollDataFromQueue(Queue<Customer> customerPriorityQueue) {
while(true){
Customer cust = customerPriorityQueue.poll();
if(cust == null) break;
System.out.println("Processing Customer with ID="+cust.getId());
}
}
}