Java Priority Queue

  • + 1 comment
    class Priorities {
    
        public List<Student> getStudents(List<String> events) {
            Comparator<Student> checker = Comparator.comparingDouble(Student::getCGPA).reversed().thenComparing(Student::getName).thenComparingInt(Student::getID);
            PriorityQueue<Student> pq = new PriorityQueue<>(checker);
            List<Student> studentList = new ArrayList<>();
            for (String event : events) {
                if (event.charAt(0) == 'E') {
                    String[] info = event.split(" ");
                    pq.add(new Student(Integer.parseInt(info[3]), info[1], Double.parseDouble(info[2])));
                } else {
                    pq.poll();
                }
            }
    
            while (!pq.isEmpty()) {
                studentList.add(pq.remove());
            }
            return studentList;
        }
    
    }
    
    class Student {
        private int id;
        private double cgpa;
        private String name;
    
        public Student(int id, String name, double cgpa) {
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
        }
    
        public int getID() {
            return this.id;
        }
    
        public double getCGPA() {
            return this.cgpa;
        }
    
        public String getName() {
            return this.name;
        }
    }