隊列的使用在日常開發中,特別常見,但是對於隊列接口中的一些方法可能使用起來有些疑惑,本文簡單記錄一下關於Queue接口中幾種類似方法的區別。
- add() 和 offer()
- add() : 添加元素,如果添加成功則返回true,如果隊列是滿的,則拋出異常
- offer() : 添加元素,如果添加成功則返回true,如果隊列是滿的,則返回false
區別:對於一些有容量限制的隊列,當隊列滿的時候,用add()方法添加元素,則會拋出異常,用offer()添加元素,則返回false
- remove() 和 poll()
- remove() : 移除隊列頭的元素並且返回,如果隊列爲空則拋出異常
- poll() : 移除隊列頭的元素並且返回,如果隊列爲空則返回null
區別:在移除隊列頭元素時,當隊列爲空的時候,用remove()方法會拋出異常,用poll()方法則會返回null
- element() 和 peek()
- element() :返回隊列頭元素但不移除,如果隊列爲空,則拋出異常
- peek() :返回隊列頭元素但不移除,如果隊列爲空,則返回null
區別 :在取出隊列頭元素時,如果隊列爲空,用element()方法則會拋出異常,用peek()方法則會返回null
附上源碼以及中文註釋:
public interface Queue<E> extends Collection<E> {
/**
* Inserts the specified element into this queue if it is possible to do so
* immediately without violating capacity restrictions, returning
* {@code true} upon success and throwing an {@code IllegalStateException}
* if no space is currently available.
* 插入一個具體的元素到隊列中如果沒有超過容量限制並且返回true。
* 如果沒有隊列已滿則拋出IllegalStateException
*
* @param e the element to add
* @return {@code true} (as specified by {@link Collection#add})
* @throws IllegalStateException if the element cannot be added at this
* time due to capacity restrictions
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* 如果插入的對象不對,則會拋出類型轉換ClassCastException
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* 如果插入null,則會拋出空指針異常
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
* 添加元素,如果添加成功則返回true,如果隊列是滿的,則拋出異常
*/
boolean add(E e);
/**
* Inserts the specified element into this queue if it is possible to do
* so immediately without violating capacity restrictions.
* When using a capacity-restricted queue, this method is generally
* preferable to {@link #add}, which can fail to insert an element only
* by throwing an exception.
* 插入一個具體的元素到隊列中如果沒有超過容量限制並且返回true。
* 如果沒有隊列已滿則返回false。
* 當使用一個有容量限制的隊列時,建議使用該方法offer(),因爲使用add()方法當隊列滿時會
* 直接拋出異常,則offer()方法則返回false
*
* @param e the element to add
* @return {@code true} if the element was added to this queue, else
* {@code false}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null and
* this queue does not permit null elements
* @throws IllegalArgumentException if some property of this element
* prevents it from being added to this queue
* 添加元素,如果添加成功則返回true,如果隊列是滿的,則返回false
*/
boolean offer(E e);
/**
* Retrieves and removes the head of this queue. This method differs
* from {@link #poll poll} only in that it throws an exception if this
* queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
* 移除隊列頭的元素並且返回,如果隊列爲空則拋出異常
*/
E remove();
/**
* Retrieves and removes the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
* 移除隊列頭的元素並且返回,如果隊列爲空則返回null
*/
E poll();
/**
* Retrieves, but does not remove, the head of this queue. This method
* differs from {@link #peek peek} only in that it throws an exception
* if this queue is empty.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
* 返回隊列頭元素但不移除,如果隊列爲空,則拋出異常
*/
E element();
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
* 返回隊列頭元素但不移除,如果隊列爲空,則返回null
*/
E peek();
}