We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java Priority Queue
Java Priority Queue
Sort by
recency
|
271 Discussions
|
Please Login in order to post a comment
`class Student{ private int id; private String name; private double cgpa;
}`
`class Priorities{ public List getStudents(List events){ PriorityQueue queue = new PriorityQueue<>( new Comparator() { public int compare(Student stu1, Student stu2){ if(stu1.getCGPA()!= stu2.getCGPA()){ return Double.compare(stu2.getCGPA(), stu1.getCGPA()); }else if (!stu1.getName().equals(stu2.getName())){ return stu1.getName().compareTo(stu2.getName()); }else { return stu1.getID() - stu2.getID(); } } } );
}`
`public class Solution {
}`
PriorityQueue is excellent when you need a dynamically sized structure that retrieves elements based on priority efficiently. But for use cases needing sorted iteration or random access, alternatives like TreeSet or ArrayList + sorting should be considered. 11xplay ID
public class Solution {
}