PriorityQueue class

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

 

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

 

PriorityQueue constructors:

PriorityQueue pq = new PriorityQueue();

  • creates a PriorityQueue object with initial default capacity of size 11, order the elements according to natural ordering

 

PriorityQueue pq = new PriorityQueue(int initialCapacity);

  • creates a PriorityQueue object with specified initial capacity, order the elements according to natural ordering

 

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

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

 

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

 

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

 

PriorityQueue pq = new PriorityQueue(PriorityQueue q);

  • creates an equivalent PriorityQueue object for the specified PriorityQueue

 

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

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
https://docs.oracle.com/javase/8/docs/api/java/util/List.html
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html
https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
https://docs.oracle.com/cd/E17802_01/j2se/j2se/1.5.0/jcp/rc/apidiffs/java/util/PriorityQueue.html

 

Happy Coding !!
Happy Learning !!