Java/J2EE and SOA (195 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (48 Blogs)
  • Comprehensive Java Course (1 Blogs)
  • Java/J2EE and SOA (195 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Implement Priority Queue In Java?

Published on Aug 13,2019 53 Views
16 / 75 Blog from Java Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

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:

  • PriorityQueue doesn’t permit NULL pointers.
  • We can’t create PriorityQueue of Objects that are non-comparable
  • PriorityQueue are unbound queues.
  • The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements — ties are broken arbitrarily.
  • The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
  • It inherits methods from AbstractQueue, AbstractCollection, Collection and Object class.

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:

Output- Priority Queue in Java- Edureka

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 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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.