优先级队列的使用
来源:互联网 发布:模拟投资基金软件 编辑:程序博客网 时间:2024/06/04 19:03
优先级队列(priority queue)中的元素可以按照任意的顺序插入,却总是按照排序的顺序进行检索。也就是说,无论何时调用remove方法,总会获得当前优先级队列中最小的元素.然后,优先级队列并没有对所有的元素进行排序。如果用迭代的方式处理这些元素,并不需要对它们进行排序。优先级队列使用了一个优雅且高效的数据结构,称为堆(heap)。堆事一个可以自我调整的二叉树,对树执行添加(add)和删除(remove)操作,可以让最小的元素移动到根,而不必花费时间对元素进行排序。
使用优先级队列的典型示例是任务调度。每一个任务都有一个优先级,任务以随机顺序添加到队列中。
PriorityQueue<GregorianCalendar> pq = new PriorityQueue<GregorianCalendar>();
pq.add(new GregorianCalendar(1906,Calendar.DECEMBER,9));
pq.add(new GregorianCalendar(1815,Calendar.DECEMBER,10));
pq.add(new GregorianCalendar(1903,Calendar.DECEMBER,3));
pq.add(new GregorianCalendar(1910,Calendar.JUNE,22));
System.out.println("Iterating over elements。。。");
for(GregorianCalendar date :pq){
System.out.println(date.get(Calendar.YEAR));
}
System.out.println("Removing elements。。。");
while(!pq.isEmpty())
System.out.println(pq.remove().get(Calendar.YEAR));
}
控制台显示:
Iterating over elements。。。
1815
1906
1903
1910
Removing elements。。。
1815
1903
1906
1910
- 优先级队列的使用
- 优先级队列的使用
- 优先级队列的使用
- 优先级队列的使用
- priority_queue 优先级队列的使用
- STL中优先级队列的使用priority_queue
- java中PriorityQueue优先级队列的使用
- 【day0414 C++】优先级队列 priority_queue的使用
- 优先级队列---使用堆
- Java 普通队列 和 优先级队列 的使用
- 优先级的队列
- 队列的优先级
- NSOperationQueue队列的优先级
- 优先级队列的实现
- 优先级队列的实现
- Python的优先级队列
- 使用c++中优先级队列
- 2617: 火热的房地产(使用优先级队列,STL库)
- java反射
- html5表单自动验证
- Java基础-条件控制语句、循环语句(3)
- List集合remove元素的问题
- Linux Crontab 设置的定时任务没有启动的排查
- 优先级队列的使用
- Java学习笔记之变量
- Follow me!百万奖金由你拿 | 精准资助数据探索(一)
- 深度学习(六)caffe入门学习
- SSL 1748_飞翔_dp+离散
- 利用UIDocumentInteractionController文档预览以及调用iPhone里第三方相关的app打开文档
- intellij idea 出现 usage search was canceled
- sdeError
- CentOS使用yum命令安装vsftp