一.优先队列的定义及其应用:
1.定义:
PriorityQueue翻译过来是优先队列,是队列中的一种特殊队列。PriorityQueue里的元素是有序的,默认情况下是单调增,可以通过传入Comparator根据自己的需求来更改。
在概念上,它是一个有序的二叉树,这种顺序的规则是(默认情况下),每个节点的值都比它的孩子节点的值小。最小堆。
在实现上,它使用数组来存储。因为该最小堆的是完全二叉树,所以按照每层从左向右的顺序存储。
2.应用:
二.优先队列的继承关系:
1.继承关系:

PriorityQueue属于集合框架下的类。它具有集合的特点,所以集合中的方法它能够使用。PriorityQueue继承自AbstractQueue,不允许插入null元素。
PriorityQueue继承自Queue, 所以在使用上 也继承了Queue的 offer()与poll()。
2.API:
基于优先级堆的无限优先级[queue](https://blog.csdn.net/qq_22596931/java/util/Queue.html) 。 优先级队列的元素根据它们的有序[natural ordering](https://blog.csdn.net/qq_22596931/java/lang/Comparable.html) ,或由一个[Comparator](https://blog.csdn.net/qq_22596931/java/util/Comparator.html)在队列构造的时候提供,这取决于所使用的构造方法。 优先队列不允许null元素。 依靠自然排序的优先级队列也不允许插入不可比较的对象(这样做可能导致ClassCastException )。
该队列的头部是相对于指定顺序的最小元素。 如果多个元素被绑定到最小值,那么头就是这些元素之一 - 关系被任意破坏。 队列检索操作poll , remove , peek和element访问在队列的头部的元件。
类及其迭代器实现[Collection](https://blog.csdn.net/qq_22596931/java/util/Collection.html)和[Iterator](https://blog.csdn.net/qq_22596931/java/util/Iterator.html)接口的所有可选方法。 方法[iterator()](https://blog.csdn.net/qq_22596931/java/util/PriorityQueue.html#iterator--)中提供的迭代器不能保证以任何特定顺序遍历优先级队列的元素。 如果需要有序遍历,请考虑使用Arrays.sort(pq.toArray()) 。
请注意,此实现不同步。 如果任何线程修改队列,多线程不应同时访问PriorityQueue实例。 而是使用线程安全的[PriorityBlockingQueue](https://blog.csdn.net/qq_22596931/java/util/concurrent/PriorityBlockingQueue.html)类。
实现注意事项:此实现提供了O(日志(n))的时间入队和出队方法( offer , poll , remove()和add ); remove(Object)和contains(Object)方法的线性时间; 和恒定时间检索方法( peek , element和size )。
三.优先队列的相关源码:
PriorityQueue内部实现方式为数组,因此优先队列的私有化参数有:
private static final long serialVersionUID = -7720805057305804111L;private static final int DEFAULT_INITIAL_CAPACITY = 11;//默认容量11transient Object[] queue;//内置的数组private int size = 0;//参数规模private final Comparator<? super E> comparator;//比较器transient int modCount = 0;
1.构造方法:
PriorityQueue的构造方法方法有:

public PriorityQueue() {this(DEFAULT_INITIAL_CAPACITY, null);}/*** Creates a {@code PriorityQueue} with the specified initial* capacity that orders its elements according to their* {@linkplain Comparable natural ordering}.** @param initialCapacity the initial capacity for this priority queue* @throws IllegalArgumentException if {@code initialCapacity} is less* than 1*/public PriorityQueue(int initialCapacity) {this(initialCapacity, null);}/*** Creates a {@code PriorityQueue} with the default initial capacity and* whose elements are ordered according to the specified comparator.** @param comparator the comparator that will be used to order this* priority queue. If {@code null}, the {@linkplain Comparable* natural ordering} of the elements will be used.* @since 1.8*/public PriorityQueue(Comparator<? super E> comparator) {this(DEFAULT_INITIAL_CAPACITY, comparator);}/*** Creates a {@code PriorityQueue} with the specified initial capacity* that orders its elements according to the specified comparator.** @param initialCapacity the initial capacity for this priority queue* @param comparator the comparator that will be used to order this* priority queue. If {@code null}, the {@linkplain Comparable* natural ordering} of the elements will be used.* @throws IllegalArgumentException if {@code initialCapacity} is* less than 1*/public PriorityQueue(int initialCapacity,Comparator<? super E> comparator) {// Note: This restriction of at least one is not actually needed,// but continues for 1.5 compatibilityif (initialCapacity < 1)throw new IllegalArgumentException();this.queue = new Object[initialCapacity];this.comparator = comparator;}/*** Creates a {@code PriorityQueue} containing the elements in the* specified collection. If the specified collection is an instance of* a {@link SortedSet} or is another {@code PriorityQueue}, this* priority queue will be ordered according to the same ordering.* Otherwise, this priority queue will be ordered according to the* {@linkplain Comparable natural ordering} of its elements.** @param c the collection whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of the specified collection* cannot be compared to one another according to the priority* queue's ordering* @throws NullPointerException if the specified collection or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(Collection<? extends E> c) {if (c instanceof SortedSet<?>) {SortedSet<? extends E> ss = (SortedSet<? extends E>) c;this.comparator = (Comparator<? super E>) ss.comparator();initElementsFromCollection(ss);}else if (c instanceof PriorityQueue<?>) {PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;this.comparator = (Comparator<? super E>) pq.comparator();initFromPriorityQueue(pq);}else {this.comparator = null;initFromCollection(c);}}/*** Creates a {@code PriorityQueue} containing the elements in the* specified priority queue. This priority queue will be* ordered according to the same ordering as the given priority* queue.** @param c the priority queue whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of {@code c} cannot be* compared to one another according to {@code c}'s* ordering* @throws NullPointerException if the specified priority queue or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(PriorityQueue<? extends E> c) {this.comparator = (Comparator<? super E>) c.comparator();initFromPriorityQueue(c);}/*** Creates a {@code PriorityQueue} containing the elements in the* specified sorted set. This priority queue will be ordered* according to the same ordering as the given sorted set.** @param c the sorted set whose elements are to be placed* into this priority queue* @throws ClassCastException if elements of the specified sorted* set cannot be compared to one another according to the* sorted set's ordering* @throws NullPointerException if the specified sorted set or any* of its elements are null*/@SuppressWarnings("unchecked")public PriorityQueue(SortedSet<? extends E> c) {this.comparator = (Comparator<? super E>) c.comparator();initElementsFromCollection(c);}
2.常用方法:
3.使用示例:
leetcode23题:合并K个升序链表
class Solution {public ListNode mergeKLists(ListNode[] lists) {//lamboda表达式初始化优先队列PriorityQueue<ListNode> q = new PriorityQueue<>((x,y)->x.val-y.val);for(ListNode node : lists){if(node!=null){q.add(node);}}ListNode head = new ListNode(0);ListNode tail = head;while(!q.isEmpty()){tail.next = q.poll();tail = tail.next;if (tail.next != null){q.add(tail.next);}}return head.next;}}
public ListNode mergeKLists(ListNode[] lists) {//内部类Queue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {@Overridepublic int compare(ListNode o1, ListNode o2) {return o1.val-o2.val;}});for (ListNode list : lists) {if (list != null){queue.offer(list);}}ListNode dummyHead = new ListNode(0);ListNode tail = dummyHead;while(!queue.isEmpty()){ListNode minNode = queue.poll();tail.next=minNode;tail = minNode;if (minNode.next!=null){queue.offer(minNode.next);}}return dummyHead.next;}

