-
[TIL] java 우선순위큐TIL 2024. 9. 20. 23:49
우선순위큐:
우선순위가 존재하는 큐.
기본적으로 큐는 선입선출형태로 나가지만, 우선순위큐는 우선순위가 높은 순서대로 나가게 된다.
우선순위큐를 쓰는이유:
자동 정렬되므로, 최소값, 최대값을 쉽게 관리할 수 있다.
여러 이벤트 중에서 우선순위가 높은 이벤트를 먼저 처리해야 할 때 사용된다
- 예시: 프로세스 스케줄링에서 우선순위가 높은 작업을 먼저 처리하는 경우.
우선순위 큐를 다루는 메서드
PriorityQueue<Integer> pq = new PriorityQueue<>(); // 선언방식1 (오름차순 기본값.) PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); //(내림차순으로 선언) pq.add(); // 값 삽입 pq.offer(); // 값 삽입. 성공시 true반환 실패시 false반환 pq.poll(); // 맨앞(우선순위 가장높은거 제거)반환하고 제거. pq.peek(); // 맨앞 반환 pq.size(); // 크기반환 pq.isEmpty(); // 큐가 비어있는지 확인 pq.clear(); // 큐안의 값 모두제거
'TIL' 카테고리의 다른 글
[TIL] 자료구조5 - 해시테이블 사용하기 (0) 2024.10.05 [TIL] java 문자열 다루는 메서드 정리 (1) 2024.09.18 [TIL] 쿼리스트링 에러 (1) 2024.08.28 오버라이딩, 오버로딩 (0) 2024.07.02 [TIL] 자료구조 - 배열, 연속리스트, 연결리스트 차이 (0) 2024.06.26