In this tutorial, we will learn about the PriorityQueue, its contructors and methods and how to implement it in Java.
Java Collection frameworks provide PriorityQueue class and it implements Queue Interface.It provides the implementation of heap data structure.
Unlink Queues, the elements are retrieved in a sorted order in PriorityQueue.
Constructors | Description |
---|---|
PriorityQueue(): | Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering. |
PriorityQueue(Collection | 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 | Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator. |
PriorityQueue(PriorityQueue | Creates a PriorityQueue containing the elements in the specified priority queue. |
PriorityQueue(SortedSet | Creates a PriorityQueue containing the elements in the specified sorted set. |
Apart from the methods inherited from its parent class/interface ,it has following methods :
Methods | Description |
---|---|
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. |
import java.util.PriorityQueue;
import java.util.Iterator;
class Main
{
public static void main(String[] args)
{
// Creating a priority queue
PriorityQueue integers = new PriorityQueue<>();
// Using the add() method
integers.add(4);
integers.add(2);
integers.add(16);
integers.add(11);
integers.add(28);
System.out.println("PriorityQueue: " + integers);
// Using the offer() method
integers.offer(1);
System.out.println("Updated PriorityQueue: " + integers);
// Using the peek() method
int number1 = integers.peek();
System.out.println("Accessed Element: " + number1);
// Using the remove() method
boolean result = integers.remove(2);
System.out.println("Is the element 2 removed? " + result);
// Using the poll() method
int number2 = integers.poll();
System.out.println("Removed Element Using poll(): " + number2);
//Using the iterator() method
Iterator iterate = integers.iterator();
while(iterate.hasNext())
{
System.out.print(iterate.next()+" ");
}
}
}
Output:
PriorityQueue: [2, 4, 16, 11, 28]
Updated PriorityQueue: [1, 4, 2, 11, 28, 16]
Accessed Element: 1
Is the element 2 removed? true
Removed Element Using poll(): 1
4 11 16 28