Queue与Deque的区别
前言
在研究java集合源码的时候,发现了一个很少用但是很有趣的点:Queue以及Deque,平常在写leetcode经常用LinkedList向上转型Deque作为栈或者队列使用,但是一直都不知道Queue的作用,于是就直接官方文档好了。
正文
概念
从上图看出,Queue以及Deque都是继承于Collection,Deque是Queue的子接口。
下面来看一下官方文档的解释。
A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue" and is usually pronounced "deck". Most
Deque
implementations place no fixed limits on the number of elements they may contain, but this interface supports capacity-restricted deques as well as those with no fixed size limit.
A collection designed for holding elements prior to processing. Besides basic
Collection
operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (eithernull
orfalse
, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restrictedQueue
implementations; in most implementations, insert operations cannot fail.
从Deque的解释中,我们可以得知:Deque是double ended queue,我将其理解成双端结束的队列,双端队列,可以在首尾插入或删除元素。而Queue的解释中,Queue就是简单的FIFO队列。
所以在概念上来说,Queue是FIFO的单端队列,Deque是双端队列。
而在使用上,又有什么差别呢?
使用
从上图我们可以得知,Queue有一个直接子类PriorityQueue,而Deque中直接子类有两个:LinkedList以及ArrayDeque。
- PriorityQueue
我觉得重点就在圈定的两个单词:无边界的,优先级的堆。然后再看看源码
在第一张图片的源码中,明显看到PriorityQueue的底层数据结构是数组,而无边界的形容,那么指明了PriorityQueue是自带扩容机制的,具体请看PriorityQueue的grow方法。
在第二张第三张图片中,可以看到插入元素的时候是需要经过compareTo的处理,那么最常用就是一些范围极值的输出,类似于堆排序的用法。
下面演示一下正反序输出三个元素的使用
private static void negativePrint(int[] nums) {
PriorityQueue<Integer> queue=new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int temp:nums){
queue.add(temp);
}
System.out.println();
System.out.print("倒序输出:");
for(int i=0;i<3;i++){
System.out.print(queue.poll()+" ");
}
}
private static void positivePrint(int[] nums){
PriorityQueue<Integer> queue=new PriorityQueue<>();
for(int temp:nums){
queue.add(temp);
}
System.out.print("正序输出:");
for(int i=0;i<3;i++){
System.out.print(queue.poll()+" ");
}
}
正序输出:1 2 3
倒序输出:9 8 8
这个在一些排行榜或者输入第N个最大/小元素会比较常用。
- LinkedList以及ArrayDeque
从官方解释来看,ArrayDeque是无初始容量的双端队列,LinkedList则是双向链表。而我们还能看到,ArrayDeque作为队列时的效率比LinkedList要高,而在栈的使用场景下,无疑具有尾结点不需判空的LinkedList较高效。
下面演示ArrayDeque作为队列以及LinkedList作为栈的使用
private static void usingAsQueue() {
Deque<Integer> queue=new ArrayDeque<>();
System.out.println("队列为空:"+queue.isEmpty()); //判断队列是否为空
queue.addLast(12); //添加元素
System.out.println("队列为空:"+queue.isEmpty()); //判断队列是否为空
System.out.println(queue.peekFirst()); //获取队列首部元素
System.out.println(queue.pollFirst()); //获取并移除栈顶元素
System.out.println("队列为空:"+queue.isEmpty()); //判断队列是否为空
}
private static void usingAsStack() {
//作为栈使用
Deque<Integer> stack=new LinkedList<>();
System.out.println("栈为空:"+stack.isEmpty()); //判断栈是否为空
stack.addFirst(12);
System.out.println("栈为空:"+stack.isEmpty()); //判断栈是否为空
System.out.println(stack.peekFirst()); //获取栈顶元素
System.out.println(stack.pollFirst()); //获取并移除栈顶元素
System.out.println("栈为空:"+stack.isEmpty()); //判断栈是否为空
System.out.println("============================================");
}
栈为空:true
栈为空:false
12
12栈为空:true
队列为空:true
队列为空:false
12
12
队列为空:true
小提示
在Deque中,获取并移除元素的方法有两个,分别是removeXxx以及peekXxx。
存在元素时,两者的处理都是一样的。但是当Deque内为空时,removeXxx会直接抛出NoSuchElementException,而peekXxx则会返回null。
所以无论在实际开发或者算法时,推荐使用peekXxx方法
其实ArrayDeque和LinkedList都可以作为栈以及队列使用,但是从执行效率来说,ArrayDeque作为队列以及LinkedList作为栈使用会是更好的选择。
另外,我在leetcode看到有人采用Vector下的Stack,这个同步加锁粒度过大(对象级),另外我觉得算法中没有线程同步的需要吧。
- 小结
PriorityQueue可以作为堆使用,而且可以根据传入的Comparator实现大小的调整,会是一个很好的选择。
ArrayDeque通常作为栈或队列使用,但是栈的效率不如LinkedList高。
LinkedList通常作为栈或队列使用,但是队列的效率不如ArrayQueue高。
总结
在java中,Queue被定义成单端队列使用,Deque被定义成双端队列使用。
而由于双端队列的定义,Deque可以作为栈或者队列使用,而Queue只能作为队列或者依赖于子类的实现作为堆使用。
本文首发于cartoon的博客
转载请注明出处:https://cartoonyu.github.io/cartoon-blog/post/java/queue%E4%B8%8Edeque%E7%9A%84%E5%8C%BA%E5%88%AB/
Queue与Deque的区别的更多相关文章
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...
- deque Comparison of Queue and Deque methods Comparison of Stack and Deque methods
1. 队列queue和双端队列deque的转换 Queue Method Equivalent Deque Methodadd(e) addLast(e)offer(e) offerLast(e)re ...
- C++ vector、list和deque的区别 (整理)
1.vector数据结构 vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内 ...
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)
向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back() 传回最后一个数据,不检查这个数据是否存在 ...
- Java中的queue和deque对比详解
队列(queue)简述 队列(queue)是一种常用的数据结构,可以将队列看做是一种特殊的线性表,该结构遵循的先进先出原则.Java中,LinkedList实现了Queue接口,因为LinkedLis ...
- python3 stack/ queue和deque模块
'''栈stack 先进后出FILO (first in last out)'''lst = []lst.append("张一山")lst.append("杨紫" ...
- [转]【Delphi】 Thread.Queue与Synchronize的区别
前话: 其实大家要学会看源码, 我接下来要说的这些东东,与其等别人讲,还不如自己搞几个代码试一下,印象还深刻点 TThread.Queue和TThread.Synchronize的区别,效果上:二者 ...
- Array,Vector,List,Deque的区别与联系【转+改】
数组 内存连续分配,长度大小固定,内置的最基础的数据结构之一.支持随机访问和随机存储. 该类型数据所占内存空间最小. Vector 是C++ STL中的一个容器.和数组类似,它拥有一段连续的内存空间, ...
- vector与deque的区别
最重要的区别,是内部实现上.deque是分段存储的. 都是支持随机存取. http://www.cnblogs.com/zhuyf87/archive/2012/12/09/2809896.html ...
随机推荐
- 数据库——MySQL乐观锁与悲观锁
乐观锁与悲观锁 一.悲观锁 悲观锁的特点是“先获取锁,再进行业务操作“”.即“悲观”的认为获取锁是非常有可能失败的,因此要先确保获取锁成功再进行业务操作 读取某几行数据时会给他们加上锁,其他的要修改数 ...
- Ubuntu安装护眼程序
目录 1.安装 2.配置 参考资料 一开始想在Ubuntu下安装在Windows下使用的f.lux,但是折腾了很久f.lux也没能正常运作.于是打开另一台Ubuntu电脑,将上面使用的Redshift ...
- Spring学习笔记(14)——注解零配置
我们在以前学习 Spring 的时候,其所有的配置信息都写在 applicationContext.xml 里,大致示例如下: java代码: <beans> <bean n ...
- 【学习总结】Python-3-算术运算符中的/和//
参考:菜鸟教程-Python3运算符 参考:菜鸟教程-Python3数字 算术运算符中的两种除法的区别: 一个斜杠/:正常的人类除法,两个int相除也保留小数 eg: 21/10 = 2.1 两个斜杠 ...
- Session的load和get方法区别是什么?
①如果没有找到符合条件的记录,get方法返回null值,而load方法会抛出异常: ②get方法直接返回实体类对象,load方法返回实体类对象的代理: ③在Hibernate3之前,get方法只在一级 ...
- cpanle/Apache 强制http跳转到https
因为租的虚拟主机是使用Cpanel,按照网上找的文章,处理的步骤如下: 打开Cpanel面板-文件管理器-设置(在页面的右上角)-勾选显示隐藏文件(dotfiles)-save . 找到网站所在的目录 ...
- java的BigDecimal比较大小
java的BigDecimal比较大小 //前提为a.b均不能为null if(a.compareTo(b) == -1){ System.out.println("a小于b"); ...
- 手写Spring事务框架
Spring事务基于AOP环绕通知和异常通知 编程事务 声明事务 Spring事务底层使用编程事务+AOP进行包装的 = 声明事务 AOP应用场景: 事务 权限 参数验证 什么是AOP技术 AO ...
- 【leetcode】994. Rotting Oranges
题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...
- 阿里云Linux服务器购买、配置
购买.配置阿里云Linux服务器配置ftp发布网站全教程 http://blog.csdn.net/Jolesen/article/details/77505840