PriorityQueue - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday 7 March 2020

PriorityQueue

In which scenarios we should use PriorityQueue.

For example, you may want to order employee records in the ascending order of their salaries or you may want to order the customers on their id’s.

We will discuss two examples of PriorityQueue – One with the default Comparator and another one with the customized comparator. .

Java PriorityQueue Example With Default Comparator :

If we don't supply comparator while creating Priority Queue, elements will be ordered in natural ascending order.


Class

import java.util.Arrays;
import java.util.Iterator;
import java.util.PriorityQueue;

public class PriorityQueueExample {

    public static void main(String[] args) {
        PriorityQueue<Integer> queue=new PriorityQueue<>();
        queue.offer(21);
        queue.offer(10);
        queue.offer(81);
        queue.offer(9);
        queue.offer(100);
        queue.offer(1);// lowest value

        //poll() method removes the head of the queue
        System.out.println(queue.poll()); // will remove the least element from the front, so here 1 is least it will be removed
        System.out.println(queue);

    }
}

Output
1 //priorityqueue removing lowest value
[9, 10, 81, 21, 100]


In below example we create PriorityQueue with using Comparator:

Here we will try to Create Employee class with two attributes name and salary, we will trt to find Employee with lowest Salary using priority Queue.


Note: To Compare Salary of two Employees we will need Comparator and after comparing salary we can remove lowest salary employee using poll() function of PriorityQueue

Class
package EmployeeWithLowestSalaryUsingPriorityQueue;

import java.util.PriorityQueue;

public class Employee {
    String name;
    int salary;

    //Creating Constructor
    public Employee(String name,int salary){

        this.name=name;
        this.salary=salary;
    }

    /*
    The toString() method returns the string representation of the object.
    If you print any object, java compiler internally invokes the toString() method on the object.
     So overriding the toString() method, returns the desired output, it can be the state of an object
     */
    @Override
    public String toString(){
        return name+" and "+salary;
    }
    public static void main(String[] args) {
        Employee employee=new Employee("Neeraj",400000);
        System.out.println(employee);
        MyComparator comparator=new MyComparator(); //Refer below class

        PriorityQueue<Employee> employeePriorityQueue=new PriorityQueue<Employee>(7,comparator);
        employeePriorityQueue.offer(new Employee("AAA",600));
        employeePriorityQueue.offer(new Employee("BBB",100));// lowest salary
        employeePriorityQueue.offer(new Employee("CCC",500));

        System.out.println(employeePriorityQueue.poll());

    }
}


Output
Neeraj and 400000
BBB and 100

Class for Comparator

package EmployeeWithLowestSalaryUsingPriorityQueue;

import java.util.Comparator;

public class MyComparator implements Comparator<Employee> {

    //compares the salary of two Employees.
    @Override
    public int compare(Employee e1, Employee e2) {
        return e1.salary-e2.salary;
    }
}



No comments:

Post a Comment