专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 问答 技术问答 poll()方法和 remove()方法的区别?

poll()方法和 remove()方法的区别?

裴裟曼依人已回答

技术点滴,Java 编程之路。

Queue队列中,poll() 和 remove() 都是从队列中取出一个元素,在队列元素为空的情况下,remove() 方法会抛出异常,poll() 方法只会返回 null 。

我们再来看一下源码的解释:

/**

* 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

*/

E poll();

 

浏览1,662技术问答
2021-03-30 16:57:15

聚焦热点问题
时时免费答疑

累计0人询问

极速问
  >>其他人还看过
更多问答

Java实验班

0学费 专业学前测评

Java就业班

围绕企业 直达就业

Java夜校班

业余时间学 超高性价比

Java架构师班

升职加薪 快速变现

返回顶部