- Extends AbstractQueue and implements the Queue interface
- Prioritized by the queue's comparator
- Dynamic: can grow
class PriorityQueueConstructors
- Can accept custom constructors
- Otherwise use default constructor -- sort the queue in ascending (从小到大) order
- offer() -- adding element to queue
- poll() -- removing element from queue
public class Main { public static void main(String[] args) { Queueq = 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,因为队列为空 } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)