PriorityQueue in Java

A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority.

Priority Queue in Java

Priority queue operates similar to a standard queue except that each element has a specific priority associated with it.

Priority queue

Priority of the elements in the PriorityQueue determines the order in which elements are removed from the PriorityQueue.

It is an Abstract DataType (ADT) and unbounded, but it has an internal capacity that governs the capacity of an array used to store the elements on the queue.

Priority queues support comparable data, i.e. the data inserted inside the queue must be able to order in some way, either from “least to greatest” or “greatest to least”, It is so that we can assign relative priorities to each element.

Priority Queue was first introduced with the JDK 1.5.

Constructors of Priority Queue

Constructors Description
PriorityQueue() It initializes a ProirityQueue with the initial default capacity of 11, and the order of the elements are according to their default ordering.
PriorityQueue(Collection c) It initializes a PriorityQueue containing the elements of the collection specified in the parameter.
PriorityQueue(int initialCapacity) It initializes a PriorityQueue with the initial capacity specified in the argument, and the order of the elements are according to its default ordering.
PriorityQueue(int initialCapacity, Comparator comparator) It creates a PriorityQueue with the initial capacity specified in the argument, and the order of the element is according to the specified comparator.
PriorityQueue(PriorityQueue<E> c) It creates a PriorityQueue containing the elements that are contained by the specified priority queue. The order of the initialized priority queue is according to the same ordering as the given priority queue.
PriorityQueue(SortedSet<E> c) It initializes the PriorityQueue containing the elements of the specified Sorted set.

Methods of Priority Queue

Modifiers/Data Type Methods Description
boolean add(E e) It adds the specified element to the priority queue.
  contains(Object o) If the queue contains the specified element in the argument, then it will return true.
  remove(Object o) First occurrence of the argumented element from the queue will be removed by this method.
  offer(E e) It inserts the element specified in the parameter into the queue.
void clear() It clears all the elements from the queue.
int size() It returns the number of entries available in the queue.
Comparator<E> comparator() It returns the comparator that orders the element in the queue or returns null if the queue is sorted according to the default ordering of its elements.
Iterator<E> Iterator() It returns an iterator for the elements in the queue.
E peek() It retrieves the top element of the queue or returns null if the list is empty.
E poll() It retrieves as well as remove the head of the queue, or returns null if the queue is empty.
Object[] toArray() It returns an array that contains all the elements in the queue.
T[] toArray(T[] a) It returns an array that contains all the elements of the queue, and the return type of the array returned is that of the specified array.

Example to add elements in the Priority Queue using add(E e) method.

Output:

Example to illustrate contains(Object o) method.

Output:

Example to remove the element from the queue using remove(Object o) method.

Output:

Example to illustrate the offer(E e) method.

Output:

Example to delete all the elements from the PriorityQueue using the clear() method.

Output:

Example to get the size of the PriorityQueue using size() method.

Output:

Example to illustrate the comparator() method.

Output:

Example to illustrate iterator() method.

Output:

Example to illustrate the peek() method.

Output:

Example to illustrate poll() method.

Output:

Example to illustrate toArray() method:

Output:

Pin It on Pinterest

Share This