How To Implement Priority Queue In Java?

Last updated on Jun 17,2021 10.8K Views

How To Implement Priority Queue In Java?

edureka.co

A Priority Queue In Java is used when the objects are supposed to be processed based on the priority. This article will help you explore this concept in detail. Following pointers will be covered in this article,

So let us get started then,

Priority Queue In Java

As mentioned already, a PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a queue follows First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. The PriorityQueue is based on the priority heap. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.Few important points on Priority Queue are as follows:

Moving on with this article on Priority Queue in Java

Queue Interface declaration

public interface Queue<E> extends Collection<E>

Moving on with this article on Priority Queue in Java

Methods of Java Queue Interface

MethodDescription

boolean add(object)

It is used to insert the specified element into this queue and return true upon success.

boolean offer(object)

It is used to insert the specified element into this queue.

Object remove()

It is used to retrieves and removes the head of this queue.

Object poll()

It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.

Object element()

It is used to retrieves, but does not remove, the head of this queue.

Object peek()

It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

Moving on with this article on Priority Queue in Java

Example

package com.journaldev.collections;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class PriorityQueueExample {
public static void main(String[] args) {		
//natural ordering example of priority queue
Queue<Integer> integerPriorityQueue = new PriorityQueue<>(7);
Random rand = new Random();
for(int i=0;i<7;i++){
integerPriorityQueue.add(new Integer(rand.nextInt(100)));
}
for(int i=0;i<7;i++){
Integer in = integerPriorityQueue.poll();
System.out.println("Processing Integer:"+in);
}
//PriorityQueue example with Comparator
Queue<Customer> customerPriorityQueue = new PriorityQueue<>(7, idComparator);
addDataToQueue(customerPriorityQueue);
pollDataFromQueue(customerPriorityQueue);
}
//Comparator anonymous class implementation
public static Comparator<Customer> idComparator = new Comparator<Customer>(){
@Override
public int compare(Customer c1, Customer c2) {
return (int) (c1.getId() - c2.getId());
}
};
//utility method to add random data to Queue
private static void addDataToQueue(Queue<Customer> customerPriorityQueue) {
Random rand = new Random();
for(int i=0; i<7; i++){
int id = rand.nextInt(100);
customerPriorityQueue.add(new Customer(id, "Pankaj "+id));
}
}
//utility method to poll data from queue
private static void pollDataFromQueue(Queue<Customer> customerPriorityQueue) {
while(true){
Customer cust = customerPriorityQueue.poll();
if(cust == null) break;
System.out.println("Processing Customer with ID="+cust.getId());
}
}
}

Output:

Thus we have come to an end of this article on ‘Priority Queue in Java’. If you wish to learn more, check out the Java Online Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES