Skip to main content

PriorityQueue 정렬 기준


1. 오름차순 우선순위 큐 (작은 값이 먼저)

PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(5);
pq.add(2);
pq.add(8);

2. 내림차순 우선순위 큐 (큰 값이 먼저)

PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);

3. 사용자 정의 객체 정렬

class Person {
    String name;
    int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

PriorityQueue<Person> pq = new PriorityQueue<>((p1, p2) -> p1.age - p2.age); // 나이 오름차순