-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueBasedOnMarks.java
More file actions
59 lines (47 loc) · 1.38 KB
/
Copy pathPriorityQueueBasedOnMarks.java
File metadata and controls
59 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package MyPractice;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class PriorityQueueBasedOnMarks {
public static class Student
{
String name;
int marks;
public Student(String name,int marks)
{
this.name=name;
this.marks=marks;
}
}
public static void main(String [] args)
{
LinkedList<Student> studentLinkedList = new LinkedList<>();
studentLinkedList.add(new Student("Tushar",99));
studentLinkedList.add(new Student("ABC",88));
studentLinkedList.add(new Student("Sachin",100));
studentLinkedList.add(new Student("Chinki",90));
PriorityQueue<Student> pq = new PriorityQueue<>(studentLinkedList.size(),new StudentComparator());
for(Student s: studentLinkedList)
{
pq.offer(s); //does not throw exception
}
while(pq.isEmpty()==false)
{
Student s = pq.poll();
System.out.println(s.name);
}
}
public static class StudentComparator implements Comparator<Student>
{
@Override
public int compare(Student a,Student b)
{
if(a.marks<b.marks)
return 1;
else if(a.marks==b.marks)
return 0;
else
return -1;
}
}
}