아래 라이브러리를 사용하면 된다.

 

import java.util.PriorityQueue;

 

			PriorityQueue<Integer> asc_queue = new PriorityQueue<Integer>();
			asc_queue.add(3);
			asc_queue.add(1);
			asc_queue.remove(1);
			asc_queue.add(5);
			asc_queue.add(4);
			asc_queue.add(2);
			asc_queue.remove(3);
			asc_queue.add(7);
			System.out.println("<최솟값 우선순위 큐>");
			System.out.println(asc_queue.poll());
			System.out.println(asc_queue.poll());
			System.out.println(asc_queue.poll());
			System.out.println(asc_queue.poll());
			PriorityQueue<Integer> desc_queue = new PriorityQueue<>(Collections.reverseOrder());
			desc_queue.add(3);
			desc_queue.add(1);
			desc_queue.remove(3);
			desc_queue.add(5);
			desc_queue.add(6);
			desc_queue.add(7);
			desc_queue.remove(7);
			desc_queue.add(2);
			System.out.println("<최댓값 우선순위 큐>");
			System.out.println(desc_queue.poll());
			System.out.println(desc_queue.poll());
			System.out.println(desc_queue.poll());
			System.out.println(desc_queue.poll());

 

결과

<최솟값 우선순위 큐>
2
4
5
7
<최댓값 우선순위 큐>
6
5
2
1

 

+ Recent posts