Priority Queue

Kablumndl
2 min readApr 13, 2021

--

From 1.5 version enhancements ( queue interface ), it is the child interface of Collection.

Collection
List (I) Set (I) Queue(I) ( Priority Queue and Blocking Queue)
Blocking Queue ( Priority Blocking Queue and Linked Blocking Queue)

If We want to represent a group of individual objects prior to processing
then we should go for queue e.g.- Before sending an SMS message to all mobile numbers we have to store them in some data structure in which order we added mobile numbers in the same order only message should be delivered.
for this first in the first-out requirement, a queue is the best choice.
usually, the queue follows first in first out the order but based on our requirement
we can implement our own priority order also ( Priority Queue )

From 1.5 version onwards LinkedList class also implements Queue interface
LinkedList based implementation of queue always follows FIFO order

There are 5 specific methods in Queue ( I )
1. boolean offer ( Object O) — add the object in the queue.
2. Object poll() — remove and return head element of the queue, if the queue is empty return null.
3. Object remove() — similar as poll() but throw NoSuchElementException of queue is null.
4. Object peek() — return head element of the queue, return null if the queue is null.
5. Object element — return head element of the queue, throw NoSuchElementException if the queue is null.

Important Features
If we want to represent a group of an individual object prior to processing
according to some priority then we should go for a priority queue.
The priority can be either default natural sorting order or defined by the comparator.
Insertion order is not preserved under it is based on some priority.
Duplicate Objects are not allowed
If we are depending on the default natural sorting order, object should compulsory homogenous and comparable otherwise we will get RuntimeException showing ClassCastException
If we are defining our own customized sorting order by comparator
then objects need not be homogenous and comparable.
Null is not allowed even is the first element of the queue.

Constructor
PriorityQueue pq = new PriorityQueue(); //Default natural sorting order
PriorityQueue pq = new PriorityQueue(int initialCapacity);
PriorityQueue pq = new PriorityQueue(int initialCapacity, Comparator c);
PriorityQueue pq = new PriorityQueue(SortedSer s);
PriorityQueue pq = new PriorityQueue(Collection c);

//Simple Program to demonstrate priority queue with default natural sorting order
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Queue Demonstation!”);
PriorityQueue pq = new PriorityQueue();
System.out.println(pq.peek()); //return null
//System.out.println(pq.element()); //return NoSuchElementExceptionat
for(int i=0; i <10; i++) {
pq.offer(i);
}
System.out.println(pq); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
System.out.println(pq.poll()); //0
System.out.println(pq.remove()); //1
}
}

//Simple Program to demonstrate priority queue with custom sorting

import java.util.*;
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Queue Demonstation!”);
PriorityQueue pq = new PriorityQueue(15, new MyComparator());
System.out.println(pq.peek()); //return null
pq.offer(“Nikhil”);
pq.offer(“Kablu”);
pq.offer(“Darshan”);
pq.offer(“Ajay”);
pq.offer(“Piyush”);


System.out.println(pq); //[Ajay, Darshan, Kablu, Nikhil, Piyush]
System.out.println(pq.poll()); //Ajay
System.out.println(pq.remove()); //Darshan
System.out.println(pq);//[Kablu, Nikhil, Piyush]


}
}
class MyComparator implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = o2.toString();
return s1.compareTo(s2);
}
}

--

--

Kablumndl
Kablumndl

Written by Kablumndl

Java Developer, Software Engineer, Spring, Spark, MicroService, PostgresSQL

No responses yet