1.什么是Deqeue
Deque的含义是“double ended queue”,双端队列,传统队列操作受限FIFO,而这个队列可以双端进出,比较灵活;
它继承自Queue的(注意这儿是接口继承接口);
public interface Deque<E> extends Queue<E> {
......
}
队列是一种特殊的线性容器,它是一种先进先出(FIFO)的数据结构。
它只允许在容器的头部进行删除操作,而在表的后端进行插入操作。进行插入操作的端成为队尾,进行删除操作的端称为队头。
当队列中没有元素时,被称为空队列。
同理当插入元素已经充满队列,被称为满队列.
再看下双端队列的Api:
可以看到扩展了不少功能,注意我划黄线的地方,可以看到它提供了一堆这种XXXfirst()
,XXXlast()
的方法。意思就是它不再是和队列一样,只能固定的FIFO的操作容器,而是既可以操作首,也可以操作尾,也可以LIFO;
而且它还有pop,push这样的方法,pop是哪儿的方法?看栈的Api:
意思就是说,依仗可以首尾操作的特性,它不仅仅可以作为一个队列来用,它还可以作为一个栈来用,并且官方也是建议用Dequeue替换Stack:
/**
* ......
* 更完整和一致的一组LIFO堆栈操作是由{@ Link Deque}接口及其实现提供应该优先使用这个类。
*
* A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre>
* {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
......
}
小结下Deque的Api:它都是根据是否抛异常提供俩套api
然后对应总结下它和栈和队列