Java Priority Queue

Sort by

recency

|

271 Discussions

|

  • + 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;
        }
    
    }
    

    }

  • + 0 comments
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.PriorityQueue;
    
    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 id;
        }
        
        public String getName() {
            return name;
        }
        
        public double getCGPA() {
            return cgpa;
        }
        
        public int compareTo(Student cStudent) {
            if (cgpa != cStudent.getCGPA()) {
                return Double.compare(cStudent.getCGPA(), cgpa);
            } else if (!name.equals(cStudent.getName())) {
                return name.compareTo(cStudent.getName());
            } else {
                return Integer.compare(id, cStudent.getId());
            }
        }
    }
    
    class Priorities {
        public List<Student> getStudents(List<String> events) {
            
            List<Student> servedStudents = new ArrayList<>();
            PriorityQueue<Student> queue = new PriorityQueue<Student>();
    
            for (String event : events) {
                String[] parsedEvent = event.split("\\s+");
                
                if ("ENTER".equals(parsedEvent[0])) {
                    Student student = new Student(
                        Integer.parseInt(parsedEvent[3]), parsedEvent[1], Double.parseDouble(parsedEvent[2]));
                    queue.add(student);
                } else if ("SERVED".equals(parsedEvent[0])) {
                    queue.poll();
                }
            }
    
            while (!queue.isEmpty()) {
                servedStudents.add(queue.poll());
            }
    
            return servedStudents;
        }
    }
    
    public class Solution {
        private final static Scanner scan = new Scanner(System.in);
        private final static Priorities priorities = new Priorities();
        
        public static void main(String[] args) {
            int totalEvents = Integer.parseInt(scan.nextLine());    
            List<String> events = new ArrayList<>();
            
            while (totalEvents-- != 0) {
                String event = scan.nextLine();
                events.add(event);
            }
            
            List<Student> students = priorities.getStudents(events);
            
            if (students.isEmpty()) {
                System.out.println("EMPTY");
            } else {
                for (Student st: students) {
                    System.out.println(st.getName());
                }
            }
        }
    }
    
  • + 1 comment
     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;
        }
    }