PriorityQueue in Java - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

PriorityQueue

 컬렉션 프레임워크로 구현된 PriorityQueue 클래스는 우선순위에 따라 객체를 처리하는 방법을 제공합니다.
 큐는 주로 선입선출 방식의 알고리즘을 따르지만, 종종 우선순위에 따라 처리해야 될 수도 있습니다. 그때 사용하는 것이 PriorityQueue입니다.
 

Example:

// PriorityQueue class
 
import java.util.*;
 
class GfG {
 
    public static void main(String args[])
    {
        // Creating empty priority queue
        Queue<Integer> pQueue
            = new PriorityQueue<Integer>();
 
        // Adding items to the pQueue
        // using add()
        pQueue.add(10);
        pQueue.add(20);
        pQueue.add(15);
 
        // Printing the top element of
        // the PriorityQueue
        System.out.println(pQueue.peek());
 
        // Printing the top element and removing it
        // from the PriorityQueue container
        System.out.println(pQueue.poll());
 
        // Printing the top element again
        System.out.println(pQueue.peek());
    }
}
 

Output:

10
10
15

 

특징

  • 우선순위 대기열에서 최대 ASCII 값이 높은 순으로 정렬
  • null을 허용하지 않음
  • PriorityQueue는 스레드로부터 안전하지 않기 때문에, 자바는 자바 멀티스레딩 환경에서 사용할 BlockingQueue 인터페이스를 구현하는 PriorityBlockingQueue 클래스를 제공
  • AbstractQueue, AbstractCollection, Collection 및 Object 클래스의 메서드를 상속

 
 

참고

 

'Java' 카테고리의 다른 글

자바프로젝트에서의 WEB과 WAS  (0) 2022.11.18
캡슐화(Encapsulation) in Java  (0) 2022.10.11
[번역] The Basics of Java Generics  (0) 2022.10.05
Java의 날짜와 시간  (0) 2022.09.28
HashMap getOrDefault(key, defaultValue) method  (0) 2022.09.27

+ Recent posts