Online Java Compiler By
JavaTpoint.com
import java.util.Comparator; import java.util.PriorityQueue; import java.util.Queue; class Student { int marks; String name; public Student(int marks, String name) { this.marks= marks; this.name = name; } public String toString(){ return this.name+" got "+ this.marks+" marks."; } } class sort implements Comparator
{ @Override public int compare(Student s1, Student s2) { if(s1.marks
s2.marks) return -1; return 0; } } public class PriorityQueuePollExample2 { public static void main(String[] args) { Student student1 = new Student(77,"Hinna"); Student student2 = new Student(87,"Vinni"); Student student3 = new Student(97,"Rima"); Queue
queue = new PriorityQueue
(1,new sort()); queue.add(student1); queue.add(student2); queue.add(student3); System.out.println(queue.poll().name+" stood First in the class"); System.out.println(queue.poll().name+" stood Second in the class"); System.out.println(queue.poll().name+" stood Third in the class"); } }
Output