![]() |
VOOZH | about |
A PriorityQueue in Java is a queue where elements are ordered based on their priority, rather than the order of insertion. By default, it uses natural ordering (min-heap), but a custom comparator can be used to define different priorities.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable, Iterable<E>
where, E is the type of elements held in this queue.
Head of Queue: 2
PriorityQueue extends AbstractQueue and implements the Queue interface, which is part of the Collection hierarchy. It also supports iteration through Iterable
This method creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
PriorityQueue<E> pq = new PriorityQueue<E>();
This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityQueue<E> pq = new PriorityQueue<E>(initialCapacity);
This creates a PriorityQueue with the default initial capacity and whose elements are ordered according to the specified comparator.
Comparator<Integer> c = (a, b) -> b - a; // max-heap
PriorityQueue<Integer> pq = new PriorityQueue<>(c);
This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue<E> pq = new PriorityQueue<E>(int initialCapacity, Comparator<E> comparator);
Letβs see how to perform a few frequently used operations on the Priority Queue class.
To add an element in a priority queue, we can use the add() method.
[0, 1, 1, 1, 2, 1]
To remove an element from a priority queue, we can use the remove() method.
Initial PriorityQueue [For, Geeks, Geeks] After Remove: [For, Geeks] Poll Method: For Final PriorityQueue: [Geeks]
To access elements from a priority queue, we can use the peek() method.
PriorityQueue: [For, Geeks, Geeks] Accessed Element: For
There are multiple ways to iterate through the PriorityQueue. The most famous way is converting the queue to the array and traversing using an iterator. Iterator does not traverse in priority order.
For Geeks Geeks
| Method | Description |
|---|---|
| add(E e) | Inserts the specified element into this priority queue. |
| clear() | Removes all of the elements from this priority queue. |
| comparator() | Returns the comparator used to order the elements in this queue or null if this queue is sorted according to the natural ordering of its elements. |
| contains(Object o) | Returns true if this queue contains the specified element. |
| forEach(Consumer<? super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
| iterator() | Returns an iterator over the elements in this queue. |
| offer(E e) | Inserts the specified element into this priority queue. |
| remove(Object o) | Removes a single instance of the specified element from this queue, if it is present. |
| removeAll(Collection<?> c) | Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
| removeIf(Predicate<? super E> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| retainAll(Collection<?> c) | Retains only the elements in this collection that are contained in the specified collection (optional operation). |
| spliterator() | Creates a late-binding and fail-fast Spliterator over the elements in this queue. |
| toArray() | Returns an array containing all of the elements in this queue. |
| toArray(T[] a) | Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array. |
| Method | Description |
|---|---|
| peek() | Retrieves, but does not remove, the head of this queue or returns null if this queue is empty. |
| poll() | Retrieves and removes the head of this queue or returns null if this queue is empty. |
| Method | Description |
|---|---|
| addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection to this queue. |
| element() | Retrieves, but does not remove, the head of this queue. |
| remove() | Retrieves and removes the head of this queue. |
| Method | Description |
|---|---|
| containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
| isEmpty() | Returns true if this collection contains no elements. |
| toString() | Returns a string representation of this collection. |
| Method | Description |
|---|---|
| containsAll(Collection<?> c) | Returns true if this collection contains all of the elements in the specified collection. |
| equals(Object o) | Compares the specified object with this collection for equality. |
| hashCode() | Returns the hash code value for this collection. |
| isEmpty() | Returns true if this collection contains no elements. |
| size() | Returns the number of elements in this collection. |