PriorityQueue
우선순위 순서로 꺼낼 수 있는 큐
Last updated
우선순위 순서로 꺼낼 수 있는 큐
Last updated
// 우선순위 큐 생성
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); // 작은 숫자가 먼저 나온다.
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Collections.reverseOrder()); // 큰 숫자가 먼저 나온다.
// user의 나이가 적은 순서대로 우선순위 큐 생성
PriorityQueue<User> priorityQueue = new PriorityQueue<>((User u1, User u2) -> u1.getAge() > u2.getAge() ? 1 : -1);
// user의 나이가 많은 순서대로 우선순위 큐 생성
PriorityQueue<User> priorityQueue = new PriorityQueue<>((User u1, User u2) -> u1.getAge() < u2.getAge() ? 1 : -1);
// 원소 추가
priorityQueue.offer(3);
priorityQueue.offer(30);
// 우선순위가 가장 높은 원소를 조회하고 삭제
Integer result = priorityQueue.poll();
// 주어진 특정 원소를 삭제
Integer result = priorityQueue.remove(3);
// 우선순위가 가장 높은 원소 조회
Integer result = priorityQueue.peek();
// 우선순위 큐 초기화
priorityQueue.clear();// 우선순위 큐 생성
PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue<String>();
PriorityBlockingQueue<String> pbq = new PriorityBlockingQueue<String>(List.of("a", "b"));
// 원소 추가
pbq.offer("hello");
pbq.offer("hi");
// 원소 삭제
pbq.remove("hi");
// 우선순위가 가장 높은 원소 조회
String result = pbq.peek();
// 우선순위 큐 원소 개수 조
int size = pbq.size();
// 우선순위 큐 초기화
pbq.clear();