A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a queue follows First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
In the below priority queue, element with maximum ASCII value will have the highest priority.
Few important points on Priority Queue are as follows:
- PriorityQueue doesn’t permit null.
- We can’t create PriorityQueue of Objects that are non-comparable
- PriorityQueue are unbound queues.
- The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements — ties are broken arbitrarily.
- The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
- It inherits methods from AbstractQueue, AbstractCollection, Collection and Object class.
Constructors of PriorityQueue class
- PriorityQueue(): Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
- PriorityQueue(Collection<E> c): Creates a PriorityQueue containing the elements in the specified collection.
- PriorityQueue(int initialCapacity): Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
- PriorityQueue(int initialCapacity, Comparator<E> comparator): Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
- PriorityQueue(PriorityQueue<E> c): Creates a PriorityQueue containing the elements in the specified priority queue.
- PriorityQueue(SortedSet<E> c): Creates a PriorityQueue containing the elements in the specified sorted set.
Below program illustrates the basic operations of PriorityQueue:
Head value using peek function:C The queue elements: C C++ Java Python After removing an elementwith poll function: C++ Python Java after removing Java with remove function: C++ Python Priority queue contains C or not?: false Value in array: Value: C++ Value: Python
Methods in PriorityQueue class:
- boolean add(E element): This method inserts the specified element into this priority queue.
- public remove(): This method removes a single instance of the specified element from this queue, if it is present
- public poll(): This method retrieves and removes the head of this queue, or returns null if this queue is empty.
- public peek(): This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
- Iterator iterator(): Returns an iterator over the elements in this queue.
- boolean contains(Object o): This method returns true if this queue contains the specified element
- void clear(): This method is used to remove all of the contents of the priority queue.
- boolean offer(E e): This method is used to insert a specific element into the priority queue.
- int size(): The method is used to return the number of elements present in the set.
- toArray(): This method is used to return an array containing all of the elements in this queue.
- Comparator comparator(): The method is used to return the comparator that can be used to order the elements of the queue.
- Implementing Dijkstra’s and Prim’s algorithms.
- Maximize array sum after K negations
This article is contributed by Mehak Kumar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- PriorityQueue add() Method in Java
- PriorityQueue contains() Method in Java
- Java.util.PriorityQueue class in Java
- PriorityQueue poll() Method in Java
- PriorityQueue size() Method in Java
- PriorityQueue spliterator() method in Java
- PriorityQueue remove() Method in Java
- PriorityQueue toArray() Method in Java
- PriorityQueue offer() Method in Java
- Implement PriorityQueue through Comparator in Java
- PriorityQueue iterator() Method in Java
- PriorityQueue peek() Method in Java
- PriorityQueue clear() Method in Java
- PriorityQueue comparator() Method in Java
- Dijkstra's shortest path algorithm in Java using PriorityQueue