Java队列
来源:互联网 发布:狸猫视频剪辑软件下载 编辑:程序博客网 时间:2024/06/04 17:50
Java容器主要是一些接口及其实现。
ArrayList和LinkedList类分别是List列表接口的数组实现和链表实现。
Java中队列容器Queue是一个接口,LinkedList实现了此接口,Queue接口窄化了对LinkedList的方法的访问权限。使用时可以利用多态将LinkedList当做队列使用。
import java.util.Queue;import java.util.LinkedList;
Queue queue = new LinkedList(); //queue只能使用Queue接口的方法
Queue方法摘要:
boolean add(E e) 将指定的元素插入此队列
boolean offer(E e) 将指定元素插入此队列
E element() 获取,但是不移除此队列的头
E remove() 获取并移除此头
E peek() 获取,但是不移除此队列的头,队空返回null
E poll() 获取并移除此队列的头,队空返回null
Queue使用时,尽量使用offer()/poll()/peek()方法。
----------------------------------------------------------------------------------------------------------------------------------
Deque
Deque是Queue的一个子接口。名称 deque 是“double ended queue(双端队列)”的缩写,通常读为“deck”。
此接口定义在双端队列两端访问元素的方法。提供插入、移除和检查元素的方法。每种方法都存在两种形式,一种在操作失败时抛出异常;另一种形式返回一个特殊值(null、false,取决于具体操作)
Queue 方法等效 Deque 方法add(e)
addLast(e)
offer(e)
offerLast(e)
remove()
removeFirst()
poll()
pollFirst()
element()
getFirst()
peek()
peekFirst()
双端队列也可用作 LIFO(后进先出)堆栈。应优先使用此接口而不是遗留 Stack
类。
原因:
For one thing, it's more sensible in terms of inheritance. The fact that Stack
extends Vector
is really strange, in my view. Early in Java, inheritance was overused IMO - Properties
being another example.
For me, the crucial word in the docs you quoted is consistent. Deque
exposes a set of operations which is all about being able to fetch/add/remove items from the start or end of a collection, iterate etc - and that's it. There's deliberately no way to access an element by position, which Stack
exposes because it's a subclass of Vector
.
Oh, and also Stack
has no interface, so if you know you need Stack
operations you end up committing to a specific concrete class, which isn't usually a good idea.
和文档中你引用的决定性的话是一致的。Deque公开了一组操作,这些操作都是能够在集合的开始或最后fetch/add/remove项,就是这个。有意的没有办法按位置访问元素,因为Stack是Vector的一个子类,所以它会暴露这个元素。
并且,Stack不是接口,所以如果你知道你需要在堆栈操作,你最终提交到一个特定的具体类中,这样是不好的。
- java队列
- java 队列
- java队列
- java 队列
- java队列
- java 队列
- java--队列
- Java 队列
- java队列
- java 队列
- java队列
- Java - 队列
- java队列
- java 队列
- java 队列
- Java队列
- 队列(JAVA)
- Java 队列
- OSI 七层模型详解
- 530. Minimum Absolute Difference in BST
- HDU4764 Stone 巴什博奕|签到
- Mongo聚合
- 树莓派3移植安卓系统指导
- Java队列
- VS2012下如何生成和调用DLL
- HDU 2580 a simple stone game K倍减法博弈游戏
- 最新面试题2017
- MVP框架简介
- 微信支付流程-公众号支付开发-视频教程5
- Spring源码(七)-IOC中的那些设计模式
- Spring源码(八)-Spring-IOC中的注解
- 科普:为什么 String hashCode 方法选择数字31作为乘子