介绍
java.util.Deque是一种双向线性集合,可以再两端对集合进行插入或者删除操作,所以既可以当队列(FIFO),也可以当栈(LIFO),Deque继承java.util.Queue。英文名介绍:A linear collection that supports element insertion and removal at both ends. The name deque is short for "double ended queue"
方法介绍
-
void addFirst(E e);
头部添加一个元素,没有容量会抛出
IllegalStateException
异常 -
void addLast(E e);
尾部添加一个元素,没有容量会抛出
IllegalStateException
异常 -
boolean offerFirst(E e);
头部添加一个元素,没有容量时会返回false
-
boolean offerLast(E e);
尾部添加一个元素,没有容量时会返回false
-
E removeFirst();
头部移除一个元素,deque为空时会抛出异常
NoSuchElementException
-
E removeLast();
尾部移除一个元素,deque为空时会抛出异常
NoSuchElementException
-
E pollFirst();
头部移除一个元素,deque为空时返回
null
-
E pollLast();
尾部移除一个元素,deque为空时返回
null
-
E getFirst();
获取一个头部元素,不移除,deque为空时抛出异常
NoSuchElementException
-
E getLast();
获取一个尾部元素,不移除,deque为空时抛出异常
NoSuchElementException
-
E peekFirst();
获取一个头部元素,不移除,deque为空时返回
null
-
E peekLast();
获取一个尾部元素,不移除,deque为空时返回
null
-
boolean removeFirstOccurrence(Object o);
删除第一个与
o
相等的元素,删除成功返回true -
boolean removeLastOccurrence(Object o);
删除最后一个与
o
相等的元素,删除成功返回true
** Queue methods **
-
boolean add(E e);
等价于
addLast
-
boolean offer(E e);
等价于
offerLast
-
E remove();
等价于
removeFirst
-
E poll();
等价于
pollFirst
-
E element();
等价于
getFirst
-
E peek();
等价于
peekFirst
-
boolean addAll(Collection<? extends E> c);
相当于每个元素分分别调用offer(E e)
** Stack methods **
-
void push(E e);
相当于
addFirst
-
E pop();
相当于
removeFirst
** Collection methods **
-
boolean remove(Object o);
相当于
removeFirstOccurrence
-
boolean contains(Object o);
是否包含某个元素
-
int size();
deque的大小
-
Iterator<E> iterator();
The elements will be returned in order from first (head) to last (tail)
-
Iterator<E> descendingIterator();
The elements will be returned in order from last (tail) to first (head).