1.创建
//最小堆
PriorityQueue<Integer> minheap = new PriorityQueue<>();
//最大堆
PriorityQueue<Integer> maxheap = new PriorityQueue<>(Collections.reverseOrder());
2.添加元素
//最小堆,[2,8,9,10,11]
minheap.add(10);
minheap.add(8);
minheap.add(9);
minheap.add(11);
minheap.add(2);
//最大堆,[11,10,9,8,2]
maxheap.add(10);
maxheap.add(8);
maxheap.add(9);
maxheap.add(11);
maxheap.add(2);
3.访问堆顶元素
//最小堆,2
minheap.peek();
//最大堆,11
maxheap.peak();
4.删除堆顶元素
//最小堆
minheap.poll();
//最大堆
maxheap.poll();
5.长度
//最小堆
minheap.size();
//最大堆
maxheap.size();
6.遍历
//边遍历边删除
while(!minheap.isEmpty())
{
minheap.poll()
}