You are viewing a single comment's thread. Return to all comments →
java 8 with streams solution:
class 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 id; } public String getName(){return name;} public double getCGPA(){return cgpa;} public String toString(){ return "id: "+ id + " name: "+ name +" cgpa: "+ cgpa; } } class Priorities{ private PriorityQueue<Student> studentQueue; private Comparator<Student> comparator = Comparator .comparingDouble(Student::getCGPA).reversed() .thenComparing(Student::getName) .thenComparingInt(Student::getID); public Priorities() { studentQueue = new PriorityQueue<>(comparator); } public List<Student> getStudents(List<String> events){ events.forEach(line ->{ String[] arr = line.split(" "); String action = arr[0]; switch(action){ case "ENTER": String name = arr[1]; double cgpa = Double.parseDouble(arr[2]); int id = Integer.parseInt(arr[3]); studentQueue.add(new Student(id, name, cgpa)); break; case "SERVED": studentQueue.poll(); break; } }); return studentQueue.stream() .sorted(comparator) .collect(Collectors.toList()); } }
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Priority Queue
You are viewing a single comment's thread. Return to all comments →
java 8 with streams solution: