Java – PriorityQueue class with example

In this article, we will discuss PriorityQueue class which orders the elements based on some priority

1. PriorityQueue:

  • PriorityQueue is a sub-class of AbstractQueue abstract class (i.e.; PriorityQueue extends AbstractQueue)
  • To represent a group of element/objects as a single unit/entity, and are ordered according to some priority, then programmer should consider using PriorityQueue
  • This priority can be natural-ordering or comparator provided customized-sorting
  • Insertion-order is NOT maintained
  • At any time, priority queue contains only unique element/objects
  • NULL insertion is NOT allowed
  • Note: basically it is doesn’t follows FIFO ordering
  • This is introduced in Java 1.5 version
  • Present in java.util package and extends java.util.AbstractQueue abstract class
41-PriorityQueue-in-java

Source: Team BenchResources.Net

2. PriorityQueue constructors:

2.1 PriorityQueue pq = new PriorityQueue();

  • creates a PriorityQueue object with initial default capacity of size 11
  • orders elements according to natural-ordering

2.2 PriorityQueue pq = new PriorityQueue(int initialCapacity);

  • creates a PriorityQueue object with specified initial capacity
  • orders elements according to natural-ordering

2.3 PriorityQueue pq = new PriorityQueue(int initialCapacity, Comparator c);

  • creates a PriorityQueue object with specified initial capacity
  • orders elements according to specified comparator

2.4 PriorityQueue pq = new PriorityQueue(SortedSet s);

  • creates an equivalent PriorityQueue object for the specified SortedSet
  • it is basically used for inter-conversion between Queue and Set objects

2.5 PriorityQueue pq = new PriorityQueue(Collection c);

  • creates an equivalent PriorityQueue object for the specified Collection
  • it is basically used for inter-conversion between Queue and Collection objects

2.6 PriorityQueue pq = new PriorityQueue(PriorityQueue q);

  • creates an equivalent PriorityQueue object for the specified PriorityQueue

3.PriorityQueue examples:

PriorityQueueExample.java

package in.bench.resources.java.collection;

import java.util.PriorityQueue;

public class PriorityQueueExample {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		PriorityQueue<String> pq = new PriorityQueue<String>();

		// adding elements to PriorityQueue object of type String
		pq.offer("Larry Page");
		pq.offer("Steve Jobs");
		pq.offer("Bill Gates");
		pq.offer("Sergey Brin");
		pq.offer("Reid Hoffman");

		System.out.println("Iterating list of founders"
				+ " in PriorityQueue\n");

		// enhanced for-loop
		for(String founder : pq){
			System.out.println(founder);
		}

		// peek and element - just retrieve head element
		String peekedElement = pq.peek();
		String headElement = pq.element();

		// printing to console
		System.out.println("\n\nHead element using peek() method : "
				+ peekedElement);
		System.out.println("Head element using element() method : "
				+ headElement);

		// poll head element - return and remove
		String polledElement = pq.poll();
		System.out.println("\n\nPoll head element using poll() "
				+ "method : " + polledElement);

		System.out.println("\nAgain, Iterating "
				+ "after poll() operations\n");

		// enhanced for-loop
		for(String founder : pq){
			System.out.println(founder);
		}

		// remove head element - return and remove
		String removeHeadElement = pq.remove();
		System.out.println("\n\nRemove head element using remove() "
				+ "method : " + removeHeadElement);

		System.out.println("\nAgain, Iterating"
				+ " after remove() operations\n");

		// enhanced for-loop
		for(String founder : pq){
			System.out.println(founder);
		}
	}
}

Output:

Iterating list of founders in PriorityQueue

Bill Gates
Reid Hoffman
Larry Page
Steve Jobs
Sergey Brin

Head element using peek() method : Bill Gates
Head element using element() method : Bill Gates

Poll head element using poll() method : Bill Gates

Again, Iterating after poll() operations

Larry Page
Reid Hoffman
Sergey Brin
Steve Jobs

Remove head element using remove() method : Larry Page

Again, Iterating after remove() operations

Reid Hoffman
Steve Jobs
Sergey Brin

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Collections a utility class for Collection
Java - Queue interface with method details