- 1 day ago
Transcript
00:00Now I have seen this, because I have stored integers, boolean, string etc. I have this syntax for that. But
00:07if I have stored objects, for example, I have given a lot of students' data. And I have added this
00:13data to my priority queue for admission. So what do I do? First of all, I have made a class.
00:20Static class Student, which I will define my data.
00:25Let's take a student to the first name of the first name of the first name of the first name
00:28of the first name of the first name of the first name of the first name of the first name
00:31of the first name of the first name of the first name of the first name of the first name
00:37of the first name of the first name of the first name of the first name of the first name
00:40of the first name of the first name of the first name of the first name of the first name
00:41of the first name of the first name of the first name of the first name of the first name
00:41of the first name of the first name of the first name of the first name of the first name
00:41of the first name of the first name of the first name of the first name of the first name
00:41of the first name of the first name of the first name of the first name of the first name
00:41of the first name of the first name of the first name of the first name of the first name
00:42of the first name of the first name of the first name of the first name of the first name
00:47of the first
00:51Now, if I give 5 students, each student has their name and rank. So, how do I define who will
00:58come first and who will come after? Because in this class there is nothing that I am telling you that
01:03I have to give the students on the rank basis. I will arrange alphabetically, whose name is A, the admission
01:10will be first. Whose name is P, the admission will be later.
01:16In classes, when you have to define priority, we need to implement a special interface. We give this interface to
01:24the name of Comparable. And who are going to compare? They are going to compare the student class objects. So
01:33what will the student class do? We will implement an interface using the implements keyword and we call this interface
01:39comparable. And who are going to compare? We compare the students.
01:43The comparable interface is in-built in Java and in the classes, because there is no in-built logic to
01:50compare, comparable gives them a special power and power, which will now compare the classes to their objects. Now, the
01:58student class means that if you want alphabetically, you can also compare on the basis of the name, on the
02:02basis of the rank, you can also compare on the basis of the rank, or compare on the basis of
02:07the role number. So, you can also compare on the basis of that basis. And how do we compare?
02:12There is a function in the comparable interface which is called compare to
02:16And if there is a function in this interface, then we also have to implement that function in the student
02:18class. How will it implement? When a parent class or a parent interface has a function, and it will implement
02:27my child class, what do we call that concept? We call that concept called function overriding. In this oops, we
02:35have already studied function overriding. When there is a function in the parent class or parent interface, when there is
02:39a function in the child class, that is overriding. And we implement that overriding.
02:45To implement the overriding first, I will write my override keyword here. There is capital O, many students will write
02:51small O or any spelling mistake. So first, add the rate, then override keyword. Then I will write public int
02:58compare to. Who are we going to compare? Student object. I will call it S2. And what will I do?
03:10My logic is that I have to sort on the rank basis. So I will return this.rank-S2.
03:17This is my compare to function, my comparable interface, my comparators. We have already studied in Greedy chapter. In reading
03:24material, there are also many questions. So, we can go there and revise it from there. I will tell you
03:31how much we are using it. So, the comparable interface has given the power of the student class to compare.
03:38How did it compare? Overriding. Which function? I have overriding this compare to function. I have to remember the syntax.
03:47we have to make notes after understanding you and remember the syntax we have to compare how to compare the
03:51class's objects. So, I have a parameter that comes to the student class's second object. What do I do with
03:59my current object? I compare this object on the basis of rank. Now, what is the logic of this line?
04:07I will tell you a little bit. I wrote a line that was this.rank-s2.rank.
04:15This, which was my this, is an object on your own. Or I can call this S1. And this S2
04:22is an object on your own. So student 2. Student 1 can have a name. Let's say this name is
04:28my ABC. And it can be 12. Student 2 can have a name like XYZ. And it can be 15.
04:36So student 1's rank is 12. Student 2's rank is 15. Now let's see what values can be.
04:44The first case in which student 1 rank is 12 and student 2 rank is 15. The value of this
04:50dot rank is 12 minus S2 rank is 15. This will give me a negative value. Negative is minus 3.
05:00This can be another case. This can be another case. Let's say that S1 rank is 15 and S2 rank
05:07is 12. In this case, this dot rank is 15 minus S2 rank is 12.
05:13This will give us a positive value. How much positive value is plus 3? It can be here if there
05:18is a plus 1, plus 4, plus 8, there is no plus positive value. And the third case will happen
05:25in which my S1 and S2 rank will be the same. Here it can be 15 here. So in this
05:30case, 15 minus 15 will always be equal. By default, in Java, this logic is fixed that if compared to
05:37function, if 0 returns, it means that my elements are equal.
05:43Then you have to keep S1 or S2. If you return a positive value compared to function, this means that
05:52my first element is greater than my second element. Or I can say that my object 1 is object 1.
06:02That is greater than my object 2. Or S1 is greater than S2.
06:09So, the object should not be big or small. The student should be big or small. But because I have
06:15defined the logic here that my student will be high when the rank is high. So, S1 will be bigger
06:21when the rank is high. And the third case in which the negative value was returned, the logic by default
06:29is that my object 1 value is less than my object 2. Or I can say my S1 is less
06:35than my S2.
06:37So this student can't be small, its height is large. But since priority is defined on the rank basis, and
06:45a negative value of rank shows that the value is less for S1 and the value is less for S2.
06:51So when sorting these values will be, then S1 will be first and S2 will be later. So in this
06:59way, I defined my student class logic for sorting. Which one should be further and which one should be later?
07:06Now, if I have many students, let's say I have 5 students. This is S1, this is S2, this is
07:14S3, this is S4, this is S5. And every student has a rank. This can be 5, this can be
07:2015, this can be 2, this can be 30, this can be 45. So what will happen? First of all,
07:25when S1 is compared to S2, what will be this.rank-S2.rank? 5-15.
07:325-15 is a negative value. Negative value means object 1 and object 2 is small. So, 5 will come
07:39first and 15 will come later. Now, 5 will compare with 2. So, what will happen? This.rank-s2.rank
07:45will be 5-2. This value will be positive. I mean, S1 is bigger than S2. So, S1 should come
07:51later. So, 2 will come first with 5. Then, 5 will compare with 30. So, what will happen in that
07:59case?
08:005-30, 5-30 means that 5 value is less than 30. Or then I will get negative value, that
08:08means that s1 is less than s2. So in this case, 30 is less than 5 after 30.
08:1530 can also come here. Now all the elements will compare with each other. And when sorting is done, what
08:22order will be in sorting? In sorting first 2, then 5, then 15, then 30, then 45. So in this
08:27way, the sorting order of objects, it defines my comparable interface using compareTo function. So this logic becomes this.rank
08:35-s2.rank. I implement this whole logic because I know that the negative value is returned, so this logic is
08:45fixed.
08:51So that is how my compare to function is implemented.
09:08Now I have a priority queue in my main function, which I have made a priority queue, I will add
09:12student objects. So I will add student to this type. I will give reverse order here. Now in PQ, I
09:22will add all students here. So I will add new student here. Let's give it a name. It will be
09:28a rank. It will be 4. In the same way, we can add students to each other. Then I have
09:40student b. It has a rank.
09:425 , then student C has its rank 2 , then student D has its rank 12 , then student
09:50D has its rank 12 . So when I peeking, I peeking and I have a student object, I will
09:56print name plus pq.peak , I will print its rank. Let's do this save and run.
10:09So this time, I will sort the student on my rank basis. First, a student came with rank 2. Then,
10:15a student came with rank 4. Then, b student came with rank 5. And last, if I take priority queue,
10:21then I have d student came with rank 12. Now, I can reverse this order too. Simply, I have to
10:26write here. Comparator.reverseOrder
10:32Comparator.ReverseOrder, now whose rank is more, that student will come first. So, in this way, we define the Priority
10:38Queue, which are all my primitive data types classes plus objects. Now, the Priority Queues are going to use very
10:46heavily in questions. You have not seen the Priority Queue, I have not defined the logic, only directly used it.
10:53So, for solving these questions, we use the PQ directly.