/** * Retrieves and removes the head of the queue represented by this deque * (in other words, the first element of this deque), or returns * <tt>null</tt> if this deque is empty. * * <p>This method is equivalent to {@link #pollFirst()}. * * @return the first element of this deque, or <tt>null</tt> if * this deque is empty */ E poll(); /** * Pops an element from the stack represented by this deque. In other * words, removes and returns the first element of this deque. * * <p>This method is equivalent to {@link #removeFirst()}. * * @return the element at the front of this deque (which is the top * of the stack represented by this deque) * @throws NoSuchElementException if this deque is empty */ E pop();
两者的区别不大,仅仅是pop()比poll()多了一个判断:元素为null,抛出异常,源码如下
if (x == null) throw new NoSuchElementException(); return x;