Java Priority Queue

  • + 0 comments
    import java[dot]util[dot]PriorityQueue ;
    import java[dot]util[dot]LinkedList ;
    class Student {
        private int id;
        private String name;
        private double cgpa;
        public Student(String name, double cgpa,int id) {
            this.id = id;
            this.name = name;
            this.cgpa = cgpa;
        }
        public int getID(){return id;}
        public String getName(){return name;}
        public double getCGPA(){return cgpa;}
    };
    class Priorities {
        private PriorityQueue<Student> queue;
        public Priorities() {
            queue = new PriorityQueue<>((s1, s2) -> {
                int[] res = {Double.compare(s2.getCGPA(),
                s1.getCGPA()),s1.getName().compareTo(s2.getName()),
                Integer.compare(s1.getID(), s2.getID())};
                for (int r : res){if (r != 0){return r;}}
                return 0;});
        }
        public List<Student> getStudents(List<String> events) {
            for (String event : events) {String[] e = event.split(" ");
                if (e[0].equals("ENTER")) 
                {queue.add(
                    new Student(e[1], Double.parseDouble(e[2]),Integer.parseInt(e[3]))
                    );}
                else if (e[0].equals("SERVED")) {queue.poll();}}
                LinkedList<Student> StudentsLeft = new LinkedList<>();
                while (!queue.isEmpty()) {StudentsLeft.add(queue.poll());}
                return StudentsLeft;}
    }