Priority queue java tutorial: From this comprehensive tutorial, we can easily grasp the concept of PriorityQueue Class of the Java collections framework by examples. Mainly, the Java PriorityQueue class Interface implements the functionality of the heap data structure. You can also use this tutorial for learning in-depth knowledge on creating a Java Priority Queue, Access PriorityQueue Elements, Constructors, and Methods of PriorityQueue Class in Java.
This Tutorial of Java Priority Queue Class Includes:
- Java PrioirityQueue
- Queue Interface Declaration
- PriorityQueue class in Java
- PriorityQueue class declaration
- Creating PriorityQueue
- Constructors of PriorityQueue in Java
- Methods of PriorityQueue Class in Java
- Access PriorityQueue Elements
- Java PriorityQueue Example
Java PrioirityQueue
Priorityqueue java example: The PriorityQueue in Java is a class that extends the AbstractQueue and implements the Queue interface. As we know that the Queue follows the FIFO manner but sometimes the elements of the Queue are processed according to the priority then the PriorityQueue comes into the picture. The important points about PriorityQueue are discussed below:
- The implementation class of Queue.
- The elements of the provided are ordered according to their natural ordering or by a comparator provided at Queue construction time.
- Null elements are not allowed.
- Not thread-safe.
- It is based upon a priority heap.
Do Check:
Queue Interface Declaration
public interface Queue<E> extends Collection<E>
PriorityQueue class in Java
Priority queue java: The PriorityQueue class gives the ease of using queue. However, it does not order the elements in a FIFO manner. It inherits AbstractQueue class.
PriorityQueue class declaration
Let’s see the declaration for java.util.PriorityQueue class.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
The class implements Serializable, Iterable<E>, Collection<E>, Queue<E> interfaces.
Where E is the type of elements held in this queue
Creating PriorityQueue
To create a priority queue in java, we should implement the java.util.PriorityQueue package. After importing the package, we can create a priority queue in java like shown below:
PriorityQueue<Integer> numbers = new PriorityQueue<>();
In the above syntax, we have created a priority queue without any arguments. In such cases, the head of the priority queue is the smallest element of the queue. However, elements are eliminated in ascending order from the queue. Also, we can personalize the ordering of elements by using the Comparator Interface.
Constructors of PriorityQueue in Java
The PriorityQueue defines the six constructors which are described below:
1. PriorityQueue(): The first constructor is used to create an empty Queue with the initial capacity 11.
2. PriorityQueue(int initialCapacity): This constructor creates a Queue with the specified initial capacity.
3. PriorityQueue(int initialCapacity, Comparator comparator): This constructor creates a Queue with the specified initial capacity and comparator.
4. PriorityQueue(Collection c): This constructor creates a PriorityQueue containing the element in the specified collection.
5. PriorityQueue(PriorityQueue q): This constructor creates a PriorityQueue containing the element in the specified PriorityQueue.
6. PriorityQueue(SortedSet ss): This constructor creates a PriorityQueue containing the element in the specified SortedSet.
Methods of PriorityQueue Class in Java
1. boolean add(E e): This method is used to add the element in the PriorityQueue. It returns false if the element is not successfully inserted.
2. boolean offer(E e): This method is the same as add() method only difference is it throws NoSuchElementException if the Queue is empty.
3. void clear(): This method is used to remove all the elements from the PriorityQueue.
4. int size(): This method returns the size of the Queue.
5. boolean remove(E e): This method is used to remove the specified element from the Queue.
6. E peek(): This method returns the element at the head of the Queue but not removed, it returns null if the Queue is empty.
7. E poll(): This method returns and removes the element at the head of the Queue and it returns null if the Queue is empty.
Access PriorityQueue Elements
In order to access elements from a priority queue, we must use the peek() method. By using this method, it returns the head of the queue. For instance, look at the below example program to access priority queue elements in Java:
import java.util.PriorityQueue; class Main { public static void main(String[] args) { // Creating a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(); numbers.add(4); numbers.add(2); numbers.add(1); System.out.println("PriorityQueue: " + numbers); // Using the peek() method int number = numbers.peek(); System.out.println("Accessed Element: " + number); } }
Output:
PriorityQueue: [1, 4, 2] Accessed Element: 1
Java PriorityQueue Example
import java.util.*; class priorityQueueExample{ public static void main(String args[]){ //creating priority queue PriorityQueue pq = new PriorityQueue(); //adding element into this queue pq.add("Amit"); pq.add("Raaj"); pq.add("Ajay"); pq.add("Vijay"); pq.add("Rahul"); //Queue methods operations System.out.println("The Queue elements are: " +pq); System.out.println("The removal element is: " +pq.remove()); System.out.println("After remove Queue elements are: " +pq); System.out.println("Queue head element is: " +pq.peek()); System.out.println("Return Queue elements: " +pq.poll()); System.out.println("After remove Queue elements are: " +pq); System.out.println("Return Queue elements without removing: " +pq.element()); System.out.println("After all operations Queue elements are: " +p); } }
Output: