PriorityQueue in Java

In this article, we will learn about PriorityQueue in Java.

A PriorityQueue is used when objects need to be processed based on priority. We know that a queue follows the first, first out the algorithm and sometimes the items in the queue need to be processed on a priority basis, then the priority queue starts. The elements of the priority row are arranged in the natural order or by the comparator provided during the construction of the row, depending on the constructor used.

Declaration

public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable

The class implements the Serializable, Iterable, Collection, Queue interfaces.

Here are some important points of the priority queue:

  • PriorityQueue does not allow null.
  • We cannot create a PriorityQueue of objects that are not comparable
  • PriorityQueue are unbound queues.
  • The head of this queue is the smallest item in terms of the order specified. If more than one element is linked at the lowest value, the head is one of those elements. The links are arbitrarily broken.
  • Because the PriorityQueue is not threaded protected, Java provides the Priority Blocking Cue class and that implements the blocking cue interface for use in multi threaded Java environments.
  • Polling operations poll, drop, examine, and access the item at the top of the queue.
  • It provides an O (log (n)) time for the add and query methods.
  • It inherits methods from the AbstractQueue, AbstractCollection, Collection, and Object classes.

Constructors

1. PriorityQueue ()
Creates a PriorityQueue with the initial default scope (11), which orders its elements in their natural order.

PriorityQueue<E> pq = new PriorityQueue<E>();

2. PriorityQueue (Collection c)
Creates a PriorityQueue that contains the elements of the specified collection.

PriorityQueue<E> pq = new PriorityQueue<E>(Collection<E> c);

3. PriorityQueue (int initialCapacity)
Creates a PriorityQueue with a specific initial capacity, which organizes its elements in their natural order.

PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity);

4. PriorityQueue (int initialCapacity, Comparator Comparator)
Creates a PriorityQueue with the specified initial capacity, which orders its elements according to the specified comparator.

PriorityQueue<E> pq = new PriorityQueue(int initialCapacity, Comparator<E> comparator);

5. PriorityQueue (PriorityQueue c)
Creates a PriorityQueue that contains the items in the specified priority queue.

PriorityQueue<E> pq = new PriorityQueue(PriorityQueue<E> c);

6. PriorityQueue (SortedSet c)
Creates a PriorityQueue that contains the elements of the specified sorted set.

PriorityQueue<E> pq = new PriorityQueue<E>(SortedSet<E> c);

Example

import java.util.*;

class Priority_Queue_Demo {

	public static void main(String args[])
	{
		// Creating empty priority queue
		PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>();

		// Adding items to the pQueue using add()
		pQueue.add(10);
		pQueue.add(20);
		pQueue.add(15);

		// Printing the top element of PriorityQueue
		System.out.println(pQueue.peek());

		// Printing the top element and removing it from the PriorityQueue container
		System.out.println(pQueue.poll());

		// Printing the top element again
		System.out.println(pQueue.peek());
	}
}

Output:

10
10
15

That’s all, we will learn about PriorityQueue in Java.

Please share this post:
Posts created 72

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: