Open In App

Custom Comparator for a Specific Element type in a PriorityQueue in Java

PriorityQueue in Java is a data structure that stores elements according to their specified natural order or comparison. By default, it uses a natural order, but we can also define customized comparisons to sort things out based on specific criteria.

Custom Comparator in PriorityQueue

A Custom Comparator in Java is an interface that defines a method for comparing two objects. It allows us to specify a custom sort for elements that do not have a natural order or when we want to override the default sort. Using a Comparator, we can provide our logic to compare items in a PriorityQueue and determine their priority.

Steps to Implement a Custom Comparator

Follow these steps to create a customized comparison for a specific element type in a PriorityQueue in Java.

Program to Implement a Custom Comparator for a Specific Element type in a PriorityQueue in Java

Below is an example of how to implement a custom comparator for a specific element type in a PriorityQueue in Java:

// Java program to Implement a Custom Comparator
import java.util.*;

// Define a class representing a Person
class Person 
{
    private String name;
    private int age;

    // Constructor to initialize name and age
    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // Getter method for name
    public String getName() 
    { 
      return name; 
    }

    // Getter method for age
    public int getAge() 
    { 
      return age; 
    }
}

// Define a custom comparator to compare Person objects
// based on age
class AgeComparator implements Comparator<Person> 
{
    @Override
    // Override the compare method to define custom
    // comparison logic
    public int compare(Person p1, Person p2)
    {
        // Compare persons based on their ages
        return p1.getAge() - p2.getAge();
    }
}

// Driver Class
public class GFG 
{
      // Main Function
    public static void main(String[] args)
    {
        // Create a PriorityQueue with a custom comparator
        // for Person objects
        PriorityQueue<Person> queue
            = new PriorityQueue<>(new AgeComparator());

        // Create some Person objects
        Person person1 = new Person("Sushmita", 25);
        Person person2 = new Person("Alekya", 30);
        Person person3 = new Person("Geetha", 20);

        // Add Person objects to the PriorityQueue
        queue.offer(person1);
        queue.offer(person2);
        queue.offer(person3);

        // Poll elements from the PriorityQueue and print
        // their details
        while (!queue.isEmpty()) 
        {
            Person person = queue.poll();
            System.out.println("Name: " + person.getName()
                               + ", Age: "
                               + person.getAge());
        }
    }
}

Output
Name: Geetha, Age: 20
Name: Sushmita, Age: 25
Name: Alekya, Age: 30



Explanation of the Program:

Article Tags :