What is Java Priority Queue with real time application example?

(Last Updated On: February 2, 2018)

Java priority queue with example and implementation will be answered. With real time application example program, the priority queue in java will be described.

Answer:

What is Priority Queue in Java?

Priority queue is just like a traditional queue with difference that when a high priority elements come to join a queue, it gets added at the head of the queue instead of tail (We know that in a queue, elements get added at the tail).

For example, let’s say, 5 elements with the same priority are in a queue. If another element with same priority comes then it will be added at tail of the queue. But, if some element comes with high priority then it will be added at the head of the queue. That’s it. This is the properties of a priority queue.

Though there are many uses of priority queue, one simple use we can consider that is thread scheduling algorithms where high priority threads start first. You can read thread priority in java. Let’s implement a simple real-time application of priority queue in java

Real time application example of priority queue in java:

Application – Patient Priority queue in a hospital: There is a queue of patients in hospital. It servers normal and emergency case. If some emergency patient come, he will be given a priority and will be treated first.

Solution: To implement it, we will use java PriorityQueue class and java comparator interface. Its Implementation is very simple and code comments has been provided. In program, below classes are implemented.

Class Patient: Patient class having patient name and priority
Class PatientPriorityComparator : Priorityqueue comparator to decide  the emergency patient
Class PriorityQueueExample – Test program for Priority queue, add and remove patient on run time and will manage priority queue.

Priority Queue Java implementation- Complete program:

This program adds the patient information at run time and display the patient list. Program allows you to enter information at run time. It gives three options i.e. option 1 – in which you can enter patent name and priority. Option:2 you can display the list of priority queue. Option :3- exit. After implementation test case examples, has been provided.

Patient class

 

Comparator implementation for priorityqueue

 

Priorityqueue java example – test program

 

TEST CASE:

Input:

Following patients are coming with priority 1 or 2(high) and they will be server in first come first serve basis but high priority patient will be treated first.

 

First, Steave with priority 1 comes
Then John with priority 1
Then Tom with priority 2( higher priority)
Then Peter with priority 1

Output: Treatment should be in order
Tom
Steave
John
Peter

Execution Result:

1.Input
2.Display list
3.Exit
enter your choice
1
Enter Patient ID:
100
Enter Patient name:
Steave
Enter priority:
1
1.Input
2.Display list
3.Exit
enter your choice
1
Enter Patient ID:
101
Enter Patient name:
John
Enter priority:
1
1.Input
2.Display list
3.Exit
enter your choice
1
Enter Patient ID:
102
Enter Patient name:
Tom
Enter priority:
2
1.Input
2.Display list
3.Exit
enter your choice
1
Enter Patient ID:
103
Enter Patient name:
Peter
Enter priority:
1
1.Input
2.Display list
3.Exit
enter your choice
2
Patient      Tom
Patient      Steave
Patient      John
Patient      Peter

1.Input
2.Display list
3.Exit
enter your choice
3