• 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();

 

  

来一道刷了进BAT的面试题?

相关文章:

  • 2022-03-03
  • 2022-12-23
  • 2021-12-29
  • 2021-12-29
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-12
  • 2021-06-24
  • 2021-12-16
  • 2021-06-02
相关资源
相似解决方案