- Extends AbstractQueue and implements the Queue interface
- Prioritized by the queue's comparator
- Dynamic: can grow
class PriorityQueue
Constructors
- Can accept custom constructors
- Otherwise use default constructor -- sort the queue in ascending (从小到大) order
Methods
- offer() -- adding element to queue
- poll() -- removing element from queue
Example
public class Main {
public static void main(String[] args) {
Queue q = new PriorityQueue<>();
// 添加3个元素到队列:
q.offer("apple");
q.offer("pear");
q.offer("banana");
System.out.println(q.poll()); // apple
System.out.println(q.poll()); // banana
System.out.println(q.poll()); // pear
System.out.println(q.poll()); // null,因为队列为空
}
}