Java PriorityQueue Class
The PriorityQueue class is a part of java collection framework. It is available inside the java.util package. The PriorityQueue class extends AbstractQueue class and implements Serializable interface.
The elements of PriorityQueue are organized as the elements of queue data structure, but it does not follow FIFO principle. The PriorityQueue elements are organized based on the priority heap.
The PriorityQueue class is used to create a dynamic queue of elements that can grow or shrunk as needed.
🔔 The PriorityQueue is a child class of AbstractQueue
🔔 The PriorityQueue implements interface Serializable.
🔔 The PriorityQueue allows to store duplicate data values, but not null values.
🔔 The PriorityQueue maintains the order of insertion.
🔔 The PriorityQueue used priority heap to organize its elements.
PriorityQueue class declaration
The PriorityQueue class has the following declaration.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
PriorityQueue class constructors
The PriorityQueue class has the following constructors.
- PriorityQueue( ) - Creates an empty PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
- PriorityQueue(Collection c) - Creates a PriorityQueue with given collection of elements.
- PriorityQueue(int initialCapacity) - Creates an empty PriorityQueue with the specified initial capacity.
- PriorityQueue(int initialCapacity, Comparator comparator) - Creates an empty PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
- PriorityQueue(PriorityQueue pq) - Creates a PriorityQueue with the elements in the specified priority queue.
- PriorityQueue(SortedSet ss) - Creates a PriorityQueue with the elements in the specified SortedSet.
Operations on PriorityQueue
The PriorityQueue class allow us to perform several operations like adding, accesing, deleting, updating, looping, etc. Let's look at each operation with examples.
Adding Items
The PriorityQueue class has the following methods to add items.
- boolean add(E element) - Appends given element to the PriorityQueue.
- boolean addAll(Collection c) - Appends given collection of elements to the PriorityQueue.
- boolean offer(E element) - Appends given element to the PriorityQueue.
Let's consider an example program to illustrate adding items to the PriorityQueue.
import java.util.*;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
PriorityQueue anotherQueue = new PriorityQueue();
queue.add(10);
queue.add(20);
queue.add(15);
System.out.println("\nQueue is " + queue);
anotherQueue.addAll(queue);
System.out.println("\nanotherQueue is " + anotherQueue);
anotherQueue.offer(25);
System.out.println("\nanotherQueue is " + anotherQueue);
}
}
Accessing Items
The PriorityQueue class has the following methods to access items.
- E element( ) - Returns the first element from the invoking PriorityQueue.
- E peek( ) - Returns the first element from the invoking PriorityQueue, returns null if this queue is empty.
Let's consider an example program to illustrate accessing items from the PriorityQueue.
import java.util.*;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
queue.add(10);
queue.add(20);
queue.add(15);
System.out.println("\nQueue is " + queue);
System.out.println("\nelement() - " + queue.element());
System.out.println("\npeek() - " + queue.peek());
}
}
Updating Items
The PriorityQueue class has no methods to update or change items.
Removing Items
The PriorityQueue class has the following methods to remove items.
- E remove( ) - Removes the first element from the invoking PriorityQueue.
- boolean remove(Object element) - Removes the first occurrence of the given element from the invoking PriorityQueue.
- boolean removeAll(Collection c) - Removes all the elements of specified collection from the invoking PriorityQueue.
- boolean removeIf(Predicate p) - Removes all of the elements of this collection that satisfy the given predicate.
- boolean retainAll(Collection c) - Removes all the elements except those are in the specified collection from the invoking PriorityQueue.
- E poll( ) - Removes the first element from the PriorityQueue, and returns null if the list is empty.
- void clear( ) - Removes all the elements from the PriorityQueue.
Let's consider an example program to illustrate removing items from the PriorityQueue.
import java.util.*;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
PriorityQueue anotherQueue = new PriorityQueue();
for(int i = 1; i <= 10; i++)
queue.add(i);
anotherQueue.add(5);
anotherQueue.add(7);
anotherQueue.add(8);
System.out.println("\nQueue initially is\n" + queue);
queue.remove();
System.out.println("\nQueue after remove() is\n" + queue);
queue.remove(8);
System.out.println("\nQueue after remove(8) is\n" + queue);
queue.poll();
System.out.println("\nQueue after poll() is\n" + queue);
queue.removeIf(n->n.equals(6));
System.out.println("\nQueue after removeIf(n->n.equals(6) is\n" + queue);
queue.retainAll(anotherQueue);
System.out.println("\nQueue after retainAll(anotherQueue) is\n" + queue);
queue.removeAll(anotherQueue);
System.out.println("\nQueue after removeAll(anotherQueue) is\n" + queue);
queue.clear();
System.out.println("\nQueue after clear() is\n" + queue);
}
}
Other utility methods
The PriorityQueue class has the following methods to work with elements of it.
- int size( ) - Returns the total number of elements in the invoking PriorityQueue.
- boolean isEmpty( ) - Returns true if the list is empty otherwise returns false.
- boolean contains(Object element) - Returns true if the list contains given element otherwise returns false.
- Object[ ] toArray( ) - Returns an array of Object instances that contains all the elements from invoking LinkedList.
- Spliterator spliterator( ) - Creates spliterator over the elements in a list.
- void trimToSize( ) - Used to trim a LinkedList instance to the number of elements it contains.
- Iterator iterator( ) - Returns an iterator over the elements in the PriorityQueue. The iterator does not return the elements in any particular order.
Let's consider an example program to illustrate other utility methods of the PriorityQueue.
import java.util.*;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();
for(int i = 1; i <= 10; i++)
queue.add(i);
System.out.println("\nQueue initially is\n" + queue);
System.out.println("\nsize() - " + queue.size());
System.out.println("\nisEmpty() - " + queue.isEmpty());
System.out.println("\ncontains(3) - " + queue.contains(3));
}
}
Consolidated list of methods
The following table providess a consolidated view of all methods of PriorityQueue.
Method | Description |
---|---|
boolean add(E element) | Appends given element to the PriorityQueue. |
boolean addAll(Collection c) | Appends given collection of elements to the PriorityQueue. |
boolean offer(E element) | Inserts the given element at end of the PriorityQueue. |
E element( ) | Returns the first element from the PriorityQueue. |
E peek( ) | Returns the first element from the PriorityQueue. |
E remove() | Removes the first element from the invoking PriorityQueue. |
boolean remove(Object element) | Removes the first occurence of the given element from the invoking PriorityQueue. |
boolean removeAll(Collection c) | Removes the given collection of elements from the invoking PriorityQueue. |
boolean removeIf(Predicate p) | Removes all of the elements of this collection that satisfy the given predicate. |
boolean retainAll(Collection c) | Removes all the elements except those are in the specified collection from the invoking PriorityQueue. |
E poll( ) | Removes the first element from the PriorityQueue, and returns null if the PriorityQueue is empty. |
void clear( ) | Removes all the elements from the PriorityQueue. |
int size( ) | Returns the total number of elements in the invoking PriorityQueue. |
boolean isEmpty( ) | Returns true if the PriorityQueue is empty otherwise returns false. |
boolean contains(Object element) | Returns true if the PriorityQueue contains given element otherwise returns false. |
Object[ ] toArray( ) | Returns an array of Object instances that contains all the elements from invoking PriorityQueue. |
Spliterator spliterator( ) | Creates spliterator over the elements in a PriorityQueue. |
void trimToSize( ) | Used to trim a PriorityQueue instance to the number of elements it contains. |