You are viewing a single comment's thread. Return to all comments →
class Student implements Comparable<Student> { private int id; private String name; private double cgpa; public Student (int id, String name, double cgpa){ this.id = id; this.name = name; this.cgpa = cgpa; } public int getId() { return this.id; } public String getName() { return this.name; } public double getCGPA(){ return this.cgpa; } @Override public int compareTo(Student other) { if (this.cgpa != other.cgpa) return Double.compare(other.cgpa, this.cgpa); int nameComp = this.name.compareTo(other.name); if (nameComp != 0) return nameComp; return this.id - other.id; } } class Priorities { public List<Student> getStudents(List<String> events) { int idStudent = 0; PriorityQueue<Student> queue = new PriorityQueue<>(); for (int i = 0; i < events.size(); i++) { if (events.get(i).startsWith("ENTER")) { String[] parts = events.get(i).split(" "); String name = parts[1]; double cgpa = Double.parseDouble(parts[2]); idStudent++; Student student = new Student(idStudent, name, cgpa); queue.add(student); } else if (events.get(i).startsWith("SERVED")) { queue.poll(); } } List<Student> result = new ArrayList<>(); while (!queue.isEmpty()) { result.add(queue.poll()); } return result; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Java Priority Queue
You are viewing a single comment's thread. Return to all comments →