Queue常用类解析之PriorityQueue

本文深入解析了PriorityQueue,一种基于最小堆数据结构的优先级队列。详细介绍了其属性,如队列元素以完全二叉树形式存储,并探讨了插入、扩容、删除元素以及上浮和下沉等核心操作的实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PriorityQueue称作优先队列,与普通队列先进先出不同,优先级队列中优先级最高的元素最先出列。

一、属性

//默认容量,初始化队列没有穿容量参数时使用
private static final int DEFAULT_INITIAL_CAPACITY = 11;

 /**
  * Priority queue represented as a balanced binary heap: the two
  * children of queue[n] are queue[2*n+1] and queue[2*(n+1)].  The
  * priority queue is ordered by comparator, or by the elements'
  * natural ordering, if comparator is null: For each node n in the
  * heap and each descendant d of n, n <= d.  The element with the
  * lowest value is in queue[0], assuming the queue is nonempty.
  */
  //元素数组
 transient Object[] queue; // non-private to simplify nested class access

 /**
  * The number of elements in the priority queue.
  */
  //元素数量
 private int size = 0;

 /**
  * The comparator, or null if priority queue uses elements'
  * natural ordering.
  */
  //优先级的比较器,仅当元素为Comparable时可以为null
 private final Comparator<? super E> com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值