We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Java
  3. Data Structures
  4. Java Priority Queue
  5. Discussions

Java Priority Queue

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 220 Discussions, By:

recency

Please Login in order to post a comment

  • sauvikd28
    4 weeks ago+ 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;
    }
    
    @Override
    public int compareTo(Student other) {
        if (this.cgpa != other.cgpa) {
            return Double.compare(other.cgpa, this.cgpa);
        } else if (!(this.name.equals(other.name))) {
            return this.name.compareTo(other.name);
        } else {
            return this.id - other.id;
        }
    }
    
    public int getID() {
        return id;
    }
    
    public String getName() {
        return name;
    }
    
    public double getCGPA() {
        return cgpa;
    }
    

    }

    class Priorities { public List < Student > getStudents(List < String > events) { PriorityQueue < Student > queue = new PriorityQueue < > ();

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

    }

    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());
            }
        }
    }
    

    }

    0|
    Permalink
  • omwaghwaghpatta
    4 weeks ago+ 0 comments

    Github Link: (https://github.com/OMWAGH9278/HackerRank/blob/main/Java/JavaPriorityQueue.java)

    0|
    Permalink
  • technologyholder
    2 months ago+ 0 comments

    Here are the solution of Java Priority Queue HackerRank Solution

    0|
    Permalink
  • syedasgarahmed11
    2 months ago+ 0 comments

    //here is code for java-8

    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.PriorityQueue;
    import java.util.Scanner;
    
    class Student {
        private final int id;
        private final String name;
        private final 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<Student> getStudents(List<String> events) {
            PriorityQueue<Student> queue = new PriorityQueue<>(
                Comparator.comparing(Student::getCgpa).reversed()
                    .thenComparing(Student::getName)
                    .thenComparing(Student::getId)
            );
            for (String event : events) {
                String[] parts = event.split("\\s+");
                if (parts[0].equals("ENTER")) {
                    String name = parts[1];
                    double cgpa = Double.parseDouble(parts[2]);
                    int id = Integer.parseInt(parts[3]);
                    Student student = new Student(id, name, cgpa);
                    queue.add(student);
                } else if (parts[0].equals("SERVED")) {
                    queue.poll();
                }
            }
            List<Student> students = new ArrayList<>(queue);
            students.sort(Comparator.comparing(Student::getCgpa).reversed()
                .thenComparing(Student::getName)
                .thenComparing(Student::getId)
            );
            return students;
        }
    }
    
    0|
    Permalink
  • adyanta_b_sriva1
    2 months ago+ 0 comments

    import java.util.PriorityQueue;

    class Student implements Comparable { 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;
    }
    
    @Override
    public int compareTo(Student other) {
        if (this.cgpa != other.cgpa) {
            return Double.compare(other.cgpa, this.cgpa);
        } else if (!this.name.equals(other.name)) {
            return this.name.compareTo(other.name);
        } else {
            return Integer.compare(this.id, other.id);
        }
    }
    

    }

    class Priorities { public List getStudents(List events) { PriorityQueue queue = new PriorityQueue();

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

    }

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy