Common /Java
PriorityQueue, Comparator
언덕너머에
2014. 5. 23. 13:58
PriorityQueue
- 우선 순위 큐로써 요소 삽입시 특정 순서에 따라 큐가 배열되는 자료구조.
예제)
PriotiryQueue<Integer> pq = new PriotiryQueue<Integer>();
pq.add(5);
pq.add(1);
pq.add(2);
System.out.println(pq.poll());
System.out.println(pq.poll());
System.out.println(pq.poll());==>출력 1 2 5
- 매서드
poll() : 큐에서 우선순위가 가장 높은 요소를 빼냅니다. 즉 반환 후에 큐에서 삭제됩니다.
peek() : poll과 달리 큐에서 삭제하지 않고 가장 우선순위가 높은 요소를 얻습니다.