JDK源码中Queue接口定义上面的注释如下:
A collection designed for holding elements prior to processing.
* Besides basic {@link java.util.Collection 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 (either {@code null} or {@code false}, depending on the
* operation). The latter form of the insert operation is designed
* specifically for use with capacity-restricted {@code Queue}
* implementations; in most implementations, insert operations cannot
* fail.
*
* <table BORDER CELLPADDING=3 CELLSPACING=1>
* <caption>Summary of Queue methods</caption>
* <tr>
* <td></td>
* <td ALIGN=CENTER><em>Throws exception</em></td>
* <td ALIGN=CENTER><em>Returns special value</em></td>
* </tr>
* <tr>
* <td><b>Insert</b></td>
* <td>{@link Queue#add add(e)}</td>
* <td>{@link Queue#offer offer(e)}</td>
* </tr>
* <tr>
* <td><b>Remove</b></td>
* <td>{@link Queue#remove remove()}</td>
* <td>{@link Queue#poll poll()}</td>
* </tr>
* <tr>
* <td><b>Examine</b></td>
* <td>{@link Queue#element element()}</td>
* <td>{@link Queue#peek peek()}</td>
* </tr>
* </table>
* ......
* ......
* ......
上面这段话从总体上描述了Queue提供的操作都有两种实现方式,一种是操作失败时抛出异常,一种是返回一个特殊值,比如null或者false。这也是remove()/poll()、add(e)/offer(e)、element()/peek()方法的区别所在。
下面的分析基于LinkedList类(实现了Queue接口)的源码
add(e)/offer(e)
一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。这时offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个unchecked 异常,而只是得到由 offer() 返回的 false。
add(E e) 返回true
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
offer(E e) 看方法体可知,是调用了add(E e)方法,可能返回false,也可能返回true
/**
* Adds the specified element as the tail (last element) of this list.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Queue#offer})
* @since 1.5
*/
public boolean offer(E e) {
return add(e);
}
remove()/poll()
remove()和poll()都是删除链表头节点,区别如下:
remove() 返回链表的头节点,如果链表为空,则抛出NoSuchElementException异常
/**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
* @since 1.5
*/
public E remove() {
return removeFirst();
}
poll() 返回链表的头节点,如果链表为空,则返回null
/**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
element()/peek()
element()和peek()方法都是返回链表头节点,区别如下:
element() 返回链表头节点,如果链表为空,则抛出NoSuchElementException异常
/**
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list
* @throws NoSuchElementException if this list is empty
* @since 1.5
*/
public E element() {
return getFirst();
}
peek() 返回链表头节点,如果链表为空,则返回null
/**
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}