The PriorityQueue
class in Java provides a priority queue data structure where elements are ordered according to their natural ordering or by a comparator provided at queue construction time. This class offers several methods to manage the elements in the priority queue.
Java PriorityQueue Methods
The table below contains various methods of the Java PriorityQueue
class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
Method | Description |
---|---|
add() | Adds the specified element to the priority queue. |
clear() | Removes all of the elements from the priority queue. |
comparator() | Returns the comparator used to order the elements in this priority queue, or null if the queue is ordered according to the natural ordering of its elements. |
contains() | Returns true if the priority queue contains the specified element. |
forEach() | Performs the given action for each element of the priority queue until all elements have been processed or the action throws an exception. |
iterator() | Returns an iterator over the elements in the priority queue. |
offer() | Inserts the specified element into the priority queue. |
offer() | Inserts the specified element into the priority queue. |
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. |
remove() | Removes a single instance of the specified element from this queue, if it is present. |
removeAll() | Removes all of the elements from this queue that are contained in the specified collection. |
removeIf() | Removes all of the elements of this collection that satisfy the given predicate. |
retainAll() | Retains only the elements in this queue that are contained in the specified collection. |
size() | Returns the number of elements in this collection. |
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. |
The PriorityQueue
class in Java provides a flexible and efficient way to manage a collection of elements with priority ordering. By understanding and utilizing these methods, developers can effectively manage and manipulate priority-based collections in their applications.
For more comprehensive details, you can visit the official Java SE documentation.