Information Security Study
우선순위 큐 개념과 예제 본문
우선순위 큐
- FIFO 구조인 기존 큐와 달리 우선순위가 높은 데이터가 먼저 나오는 구조
- 모든 값에 우선 순위가 있다.
- 들어간 순서와 상관 없이 높은 우선순위부터 처리
public static void main(String[] args) {
// 기본 - 오름차순
PriorityQueue<Integer> pqueue = new PriorityQueue<>();
// 내림차순
// PriorityQueue<Integer> pqueue = new PriorityQueue<>(Collections.reverseOrder());
pqueue.add(1);
pqueue.add(4);
pqueue.add(2);
pqueue.offer(8);
while (!pqueue.isEmpty()) {
System.out.println(pqueue.poll());
}
}
/* 실행 결과
1
2
4
8
*/
'Programming > JAVA' 카테고리의 다른 글
해시맵 개념과 예제 (0) | 2024.09.17 |
---|---|
Deque 개념 및 예제 (0) | 2024.09.17 |
[프로그래머스] 기능개발(level 2, Queue) (0) | 2024.09.10 |
[프로그래머스] 올바른 괄호(level2, Stack) (0) | 2024.09.10 |
[프로그래머스] 프로세스(level2, Queue) (0) | 2024.09.04 |