Java Priority Queue

  • + 0 comments

    the exact code :

    import java.util.*; class Priorities {

    List<Student> getStudents(List<String> events) {
    
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(10,new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                return s1.getCGPA() != s2.getCGPA() ? s2.getCGPA() - s1.getCGPA() > 0 ? 1 : -1 : !s1.getName().equals(s2.getName()) ? s1.getName().compareTo(s2.getName()) :  s1.getID() > s2.getID() ? 1 : -1;
            }
        });
    
        for (String str: events) {
             String isENTER = str.split(" ")[0];
    
            if (isENTER.equals("ENTER")) {
                int id = Integer.parseInt(str.split(" ")[3]);
                String name = str.split(" ")[1];
                double cgpa = Double.parseDouble(str.split(" ")[2]);
    
                priorityQueue.add(new Student(id, name, cgpa));
    
            } else priorityQueue.poll();
        }
    
        List<Student> result = new ArrayList<>();
    
        while (!priorityQueue.isEmpty()) {
            result.add(priorityQueue.poll());
        }
    
        return result;
    }
    

    }

    class Student { private int id; private String name; private double cgpa;

    Student(int id, String name, double cgpa) {
        this.id = id;
        this.name = name;
        this.cgpa = cgpa;
    }
    
    int getID() {
        return id;
    }
    
    String getName() {
        return name;
    }
    
    double getCGPA() {
        return cgpa;
    }
    

    }