Just by name "Queue" we can think of big "Q" for Movie tickets booking in our olden days or have read about FIFO order. But here "PriorityQueue" is little different and it follows same Queue functionality with Priority items are out first.
Lets take scenario where there are lot of incoming tickets in support system with multiple priorities (CRITICAL, HIGH, MEDIUM, LOW). Here support team needs to serve all tickets based on their priority than their logged order, like critical priorities first and then high, medium and low priority tickets at last.
Lets see simple example class which takes multiple Tickets along with priority and while we are reading critical tickets 1st, high tickets 2nd, medium tickets 3rd and low priority tickets at last.
Ticket.java
PriorityQueueExample.java
OUTPUT:
Here in output we can see list of tickets printed by priority order wise.
Lets take scenario where there are lot of incoming tickets in support system with multiple priorities (CRITICAL, HIGH, MEDIUM, LOW). Here support team needs to serve all tickets based on their priority than their logged order, like critical priorities first and then high, medium and low priority tickets at last.
Lets see simple example class which takes multiple Tickets along with priority and while we are reading critical tickets 1st, high tickets 2nd, medium tickets 3rd and low priority tickets at last.
Ticket.java
/* * Simple Ticket class takes input as ticket number, Ticket details * along with priority of each ticket */ public class Ticket implements Comparable<Ticket>{ enum Priority{ CRITICAL, HIGH, MEDIUM, LOW } private int ticketId; private String task; private Priority priority; public Ticket(int ticketId, Priority priority, String task) { this.ticketId = ticketId; this.task = task; this.priority = priority; } @Override public String toString() { return ticketId +" : "+task; } @Override public int compareTo(Ticket o) { return this.priority.compareTo(o.priority) ; } }
PriorityQueueExample.java
import java.util.PriorityQueue; import java.util.Queue; import com.pract.Ticket.Priority; public class PriorityQueueExample { public static void main(String[] args) { // Adding 6 Tickets with different priority Queue<Ticket> ticketQueue = new PriorityQueue<Ticket>(); ticketQueue.offer(new Ticket(1, Priority.LOW, "Low priority task - Cleanup activity")); ticketQueue.offer(new Ticket(2, Priority.MEDIUM, "Medium priority task - Taking dump")); ticketQueue.offer(new Ticket(3, Priority.HIGH, "High priority task - Production update")); ticketQueue.offer(new Ticket(4, Priority.MEDIUM, "Medium priority task - Testing")); ticketQueue.offer(new Ticket(5, Priority.HIGH, "High priority task - Important bug fixing")); ticketQueue.offer(new Ticket(6, Priority.CRITICAL, "Critical priority task - Hacker Thread")); listTicketsBasedOnPriority(ticketQueue); } /* * Listing tickets based on priority like * CRITICAL, HIGH, MEDIUM and then LOW tickets. */ private static void listTicketsBasedOnPriority(Queue<Ticket> tickets){ while (!(tickets.isEmpty())) System.out.println(tickets.poll()); } }
OUTPUT:
6 : Critical priority task - Hacker Thread 5 : High priority task - Important bug fixing 3 : High priority task - Production update 2 : Medium priority task - Taking dump 4 : Medium priority task - Testing 1 : Low priority task - Cleanup activity
Here in output we can see list of tickets printed by priority order wise.
No comments:
Write comments