Java Essentials - Priority queue in java
7K views
Oct 24, 2024
Java Essentials - Priority queue in java
View Video Transcript
0:00
In this video we are discussing priority Q in Java
0:04
So this priority Q, like the previous video we have discussed Q, this priority Q does not support
0:10
that first in first out or last in last out data structure
0:14
It arranges our data according to the priority, based on the priority
0:19
So now let me discuss more on this priority queue in our discussion
0:24
So the priority Q in Java collection, the priority Q does not follow the FIFO rule
0:29
unlike the normal queue with whatever we have discussed in the earlier video
0:34
So it arranges the elements based on their priorities. So depending upon the priorities, the elements will be arranged
0:41
Priority queue can store comparable objects to arrange them in the correct priorities
0:48
It has the method like add, remove, etc. And also it has pole method that is used to retrieve and remove the element from the head of the queue
0:59
So these are the different methods we have discussed. I think to clear our conception in a better way, let us go for one practical demonstration
1:07
where it will be discussing more on this priority queue. The demonstration is coming next
1:13
When you define one class which is implementing comparable interface, you can write a
1:19
sample code for you. So class, say ST implements. comparable so here I putting this one as ST then you can find that as it is one interface it is having some unimplemented methods if you go on adding this unimplemented method we getting the method that is a compared to so compared to will take the class object
1:49
ST class object as input and it returns one integer so we are supposed to write
1:54
the rewrite the code or the body for this compared to method so we have applied
1:59
the same here so let me go for my coding so class student implements comparable
2:05
student so private I nt rank and private string name so these two are instance
2:11
variable one is a rank another one is the name of the type I and string class
2:16
objects so this is our student is the constructor which will take this name and
2:20
rank as input parameters and they will get input arguments and they will
2:25
instantiate this instance variable that is that this dot rank and this dot name
2:31
now this is a comparative which is there unimplemented this interface that is a comparable so comparable is nothing but one
2:39
interface you can find it here and it is defined under the package is java
2:42
dot lang so these are compared to we have written so this is about a input
2:47
argument will be one student class object so let it be ST so it will compare
2:52
the rank of the student with another student so if rank is less than
2:57
ST dot rank then return minus one else if rank is greater than
3:02
st.d rank then return one otherwise return zero so depending upon the value of this this dot rank and ST dot rank
3:10
the either minus 1 or 1 or 0 will be returned as I have shown here also we are overriding the method that is a two string so there is a string result is equal to student name concatenation name concordination rank and concordination
3:26
rank here and return results so string will be returned as the output
3:30
argument for this two string method so now here we have defined one class that is
3:35
a priority queue under this class we have defined the main function so priority
3:39
Q class object there is a student Q the and which will hold the student class
3:45
objects as its elements so student class we have defined here so it is nothing but
3:50
implementing the comparable interface and we also have overridden and the respective method compared to so priority Q is not nothing but the name of the
4:01
class and then student is the the respective objects which will be which will be
4:06
deciding in this priority Q object that is a student Q as items and is equal to
4:12
new priority queue we are calling the constructor here so student Q volte add so using this ad method we can go on adding or inserting new students into this priority Q so here we are having this Ashish there is a name and five is the respective role a rank rather you can say so here you see this this is my rank will be there and the name is there at first I'm passing this name as input parameter and rank as the second parameter here so name is Ashish rank is five name is borun rank is three you can
4:45
easily call that this student is actually implementing this comparable interface so
4:50
what will happen when they will get inserted so they will remain short it
4:54
accordingly depending upon the rank of the student so here you see we are here we have added five such student items here so insert students into the priority queue and part when we shall go for the
5:08
the printing of its content will be finding them in the short-ed order so we
5:13
shall come that point later so at first we are going to print the size of this
5:17
student queue as we have inserted five items so the size of the queue will be
5:22
five here now we are creating one iterator to iterate through this queue so iterator IT is equal to student q
5:32
iterator now we know that we are having the method that is a has next which is
5:37
returning a bullion so IT dot has next so when they it is true then we are
5:43
printing this student q.poll dot two strings so in this way you are just
5:47
printing this one so student q.poll so we are retrieving each and every
5:52
element from here for each and every iteration and then we are just calling this method two string which we have over-readed here which will
6:01
print the name and the ranked accordingly remember we entered and inserted and this
6:07
new elements got added to this student queue in the order that is a sish 5 then
6:12
boron 3 then Monish 1 then show week 4 and Rahul 2 so in the execution you
6:18
are finding that they're coming like the Monish 1 with the rank 1 has come at the
6:23
first then Rahul with the rank 2 then boron with the rank 2 then borun with the 3 then show week with the rank 4 and Ashish with the respective rank 5 here in this
6:33
way we have given you the idea that how to deal with the priority Q class
6:38
objects in our Java code thanks for watching this video
#Java (Programming Language)