Java PriorityQueue with Comparator

By Arvind Rai, December 21, 2022
On this page, we will learn to use java.util.PriorityQueue with specified Comparator in our Java application.
1. The PriorityQueue is an unbounded queue based on a priority heap. The PriorityQueue has a default initial capacity or we can specify its initial capacity. As elements are added to a priority queue, its capacity grows automatically.
2. The elements of the PriorityQueue are ordered according to their natural ordering or by Comparator specified to the constructor at creation time. The PriorityQueue that depends on natural ordering, does not permit insertion of non-comparable objects.

Constructors with Comparator

Find the constructors of PriorityQueue with Comparator arguments.
1.
public PriorityQueue(int initialCapacity,
             Comparator<? super E> comparator) 
Creates a PriorityQueue using specified initial capacity and the comparator to order the elements. If comparator is null, elements will be ordered in natural order.
Parameters:
The initialCapacity is the initial capacity for this priority queue.
The comparator is the comparator that will be used to order this priority queue.
Throws:
If initial capacity is less than 1, IllegalArgumentException is thrown.

2.
public PriorityQueue(Comparator<? super E> comparator) 
Creates a PriorityQueue using default initial capacity and the specified comparator to order the elements. If comparator is null, elements will be ordered in natural order.
Parameters:
The initialCapacity is the initial capacity for this priority queue.

Using Comparator as Lambda Expression

PriorityQueueOne.java
package com.concretepage;
import java.io.IOException;
import java.util.Comparator;
import java.util.PriorityQueue;

public class PriorityQueueOne {
  public static void main(String... args) throws Exception, IOException {
	Comparator<Person> nameComparator = Comparator.comparing(Person::getName);
	PriorityQueue<Person> pq = new PriorityQueue<>(nameComparator);
	Person p1 = new Person("Mohan", 25);
	Person p2 = new Person("Krishn", 30);
	Person p3 = new Person("Abhay", 32);
	pq.add(p1);
	pq.add(p2);
	pq.add(p3);
	System.out.println(pq);
  }
} 
Person.java
package com.concretepage;
public class Person {
  private String name;
  private int age;
  public Person(String name, int age) {
	this.name = name;
	this.age = age;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public String toString() {
    return name + " - " + age; 
  }
} 
Output
[Abhay - 32, Mohan - 25, Krishn - 30] 

Using Comparator Class

PriorityQueueTwo.java
package com.concretepage;
import java.io.IOException;
import java.io.Serializable;
import java.util.Comparator;
import java.util.PriorityQueue;

public class PriorityQueueTwo {
  public static void main(String... args) throws Exception, IOException {
	NameComparator nameComparator = new NameComparator();
	PriorityQueue<Person> pq = new PriorityQueue<>(nameComparator);
	Person p1 = new Person("Mohan", 25);
	Person p2 = new Person("Krishn", 30);
	Person p3 = new Person("Abhay", 32);
	pq.add(p1);
	pq.add(p2);
	pq.add(p3);

	System.out.println(pq);
  }
}
class NameComparator implements Comparator<Person>, Serializable {
  private static final long serialVersionUID = 1L;
  @Override
  public int compare(Person p1, Person p2) {
     return p1.getName().compareTo(p2.getName());
  }
} 
Output
[Abhay - 32, Mohan - 25, Krishn - 30] 

With Natural Ordering

The PriorityQueue constructors without Comparator, are ordered according to their natural ordering. Find the example of PriorityQueue that depends on natural ordering.
PriorityQueue<String> pq = new PriorityQueue<>();
pq.add("Mohan");
pq.add("Krishn");
pq.add("Abhay");
System.out.println(pq); 
Output
[Abhay, Mohan, Krishn] 
If we add non-comparable objects, it will throw ClassCastException .
PriorityQueue<Person> pq = new PriorityQueue<>();
Person p1 = new Person("Mohan", 25);
Person p2 = new Person("Krishn", 30);
Person p3 = new Person("Abhay", 32);
pq.add(p1);
pq.add(p2);
pq.add(p3); 
Output
Exception in thread "main" java.lang.ClassCastException 

Reference

Class PriorityQueue
POSTED BY
ARVIND RAI
ARVIND RAI











©2023 concretepage.com | Privacy Policy | Contact Us