Java Priority Queue

Sort by

recency

|

273 Discussions

|

  • + 0 comments

    I don't know if I'm the only one who's having this issue, but I can't import the "PriorityQueue" class:

    PriorityQueue cannot be resolved to a typeJava(16777218)

  • + 0 comments

    A Java PriorityQueue is a data structure from the Java Collections Framework that functions like a regular queue but orders its elements based on priority rather than insertion order. Winbuzz Sign Up

  • + 0 comments

    `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;
    }
    

    }`

    `class Priorities{ public List getStudents(List events){ PriorityQueue queue = new PriorityQueue<>( new Comparator() { public int compare(Student stu1, Student stu2){ if(stu1.getCGPA()!= stu2.getCGPA()){ return Double.compare(stu2.getCGPA(), stu1.getCGPA()); }else if (!stu1.getName().equals(stu2.getName())){ return stu1.getName().compareTo(stu2.getName()); }else { return stu1.getID() - stu2.getID(); } } } );

        for (String e : events){
            String[] part = e.split(" ");
            if(part[0].equals("ENTER")){
                String name = part[1];
                double cgpa = Double.parseDouble(part[2]);
                int id = Integer.parseInt(part[3]);
                queue.add(new Student(id, name, cgpa));
            }else if (part[0].equals("SERVED")){
                queue.poll();
            }
        }
    
        List<Student> result = new ArrayList<>();
        while (!queue.isEmpty()){
            result.add(queue.poll());
        }
        return result;
    }
    

    }`

    `public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System.in);
        int n = Integer.parseInt(input.nextLine());
    
        List<String> events = new ArrayList<>();
        while (n-- > 0){
            events.add(input.nextLine());
        }
    
        Priorities priorities = new Priorities();
        List<Student> students = priorities.getStudents(events);
    
        if(students.isEmpty()){
            System.out.println("EMPTY");
        }else {
            for (Student s : students){
                System.out.println(s.getName());
            }
        }
    }
    

    }`

  • + 0 comments

    PriorityQueue is excellent when you need a dynamically sized structure that retrieves elements based on priority efficiently. But for use cases needing sorted iteration or random access, alternatives like TreeSet or ArrayList + sorting should be considered. 11xplay ID

  • + 0 comments

    public class Solution {

    private final static Scanner sc = new Scanner(System.in);
    private final static Priorities priorities = new Priorities();
    
    public static void main(String[] args) {
    
       int totalEventos = Integer.parseInt(sc.nextLine().trim());
       List<String> events = new ArrayList<>();
    
       for (int i=0; i < totalEventos; i++){
        events.add(sc.nextLine().trim());
       }
    
        List<Student> students = priorities.getStudents(events);
    
        if (students.isEmpty()) {
                System.out.println("EMPTY");
        } else {
            for (Student s : students) {
                System.out.println(s.getName());
            }
        }
       sc.close();
    }
    
    static 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;
        }
    }
    
    static class Priorities {
    
        public List<Student> getStudents(List<String> events){
            PriorityQueue<Student> fila = new PriorityQueue<>(
                    Comparator.comparing(Student::getCGPA).reversed()
                    .thenComparing(Student::getName)
                    .thenComparing(Student::getID)
            );
    
            for(String enter : events) {
                String[] result = enter.split(" ");
    
                if(result[0].equalsIgnoreCase("ENTER")){
                        fila.add(new Student(Integer.parseInt(result[3]), 
                        result[1], 
                        Double.parseDouble(result[2])
                        ));
                } else if (result[0].equals("SERVED")) {
                        fila.poll();
                }
            }
    
            List<Student> restantes = new ArrayList<>();
            while(!fila.isEmpty()) {
                    restantes.add(fila.poll());
            }
            return restantes;
        }
    
    }
    

    }