- Queue 中 element() 和 peek() 都是用來返回隊列的頭元素,不刪除。
- 在隊列元素為空的情況下,element() 方法會拋出NoSuchElementException異常,peek() 方法只會返回 null。
JDK1.8 中源碼解釋
/**
* 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
*/
E peek();