PriorityQueue in Java with Example

The PriorityQueue in Java is a class which extends the AbstractQueue and implements the Queue interface. As we know that the Queue follows FIFO manner but sometimes the elements of the Queue are processed according to the priority than the PriorityQueue comes into the picture. The important points about PriorityQueue are discussed below:

1. implementation class of Queue.
2. The elements of the provided are ordered according to their natural ordering or by a comparator provided at Queue construction time.
3. Null elements are not allowed.
4. Not thread safe.
5. It is based upon priority heap.


PriorityQueue in Java


Constructors in Java PriorityQueue:

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 Java PriorityQueue:

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 remove the element at the head of the Queue and it returns null if the Queue is empty.

Java PriorityQueue Example:


import java.util.*;
class priorityQueueExample{
     public static void main(String args[]){ 

//creating priority queue 
PriorityQueue<String> pq = new PriorityQueue<String>(); 

 //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:
PriorityQueue in Java with Example




You may also like these related posts:
PriorityQueue in Java with Example PriorityQueue in Java with Example Reviewed by Prashant Srivastava on March 15, 2019 Rating: 5

No comments:

Powered by Blogger.