为什么Java提供两种方法从队列中删除元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2193450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why Java provides two methods to remove element from Queue?
提问by munish
The Queue
implementation in Java has two methods to remove element, One is remove()
which throws exception and other one is poll()
which returns null
for an empty queue. I have two doubts:
Queue
Java 中的实现有两种删除元素的方法,一种是remove()
抛出异常,另一种是poll()
返回null
空队列。我有两个疑惑:
- Why
Queue
has different implementation to remove element? - Which implementation to use When?
- 为什么
Queue
有不同的实现来删除元素? - 何时使用哪个实现?
回答by Mykola Golubyev
When you know how to react right now and/or expect elements to be absent then use poll.
当您知道如何立即做出反应和/或期望元素不存在时,请使用 poll。
Otherwise use remove.
否则使用删除。
回答by Poindexter
Sometimes you want a null value returned for an empty queue and sometimes you want it to treat an empty queue as a exception case.
有时您希望为空队列返回空值,有时您希望它将空队列视为异常情况。
回答by Jon
The two methods are used differently in classic discussions about a Queue structure. I use poll() mostly to retrieve items, and remove() mostly if I need to modify the Queue outside of the normal loop.
在有关队列结构的经典讨论中,这两种方法的使用方式不同。我主要使用 poll() 来检索项目,而 remove() 主要用于在正常循环之外修改队列。
回答by Theo
In some situations it's expected that a queue will be empty, and in those cases having a method that doesn't throw an exception is appropriate. In other situations it's an exceptional circumstance that the queue is empty, and an exception is appropriate.
在某些情况下,预计队列将为空,在这些情况下,使用不引发异常的方法是合适的。在其他情况下,队列为空是一种特殊情况,异常是合适的。
Throwing exceptions incurs a performance penalty, and if it's the case that you expect the queue to be empty from time to time you don't want to have to handle the queue-empty-logic as catching an exception -- it's both costly and difficult to read.
抛出异常会导致性能损失,如果您希望队列不时为空,您不希望将队列空逻辑处理为捕获异常——这既昂贵又困难阅读。
In the opposite case where you don't expect the queue to ever be empty it is a sign of a programming error, or some other exceptional circumstance that it is, and you don't want to write ugly error condition checking code (e.g. checking for null), because in this case that would be less readable than catching an exception (which you can do in another scope).
在相反的情况下,您不希望队列永远为空,这是编程错误的标志,或者是其他一些特殊情况,并且您不想编写丑陋的错误条件检查代码(例如检查为空),因为在这种情况下,这比捕获异常(您可以在另一个范围内执行)可读性差。
回答by Massimo Fazzolari
The abstract class AbstractQueue<E>
implements Queue<E>
and define the remove method.
抽象类AbstractQueue<E>
实现Queue<E>
并定义了 remove 方法。
You can take a look at source code:
你可以看看源代码:
public E remove() {
E x = poll();
if (x != null)
return x;
else
throw new NoSuchElementException();
}
So, as you can see, the remove()
method usepoll()
method.
因此,如您所见,remove()
方法使用poll()
方法。
You can use which one you prefer.
您可以使用您喜欢的一种。
回答by Achow
Looking at the answers, it wasn't clear to me which did what,hence:
查看答案,我不清楚哪个做了什么,因此:
Straight from the API :The remove() and poll() methods differ only in their behavior when the queue is empty: the remove() method throws an exception, while the poll() method returns null
直接来自 API:remove() 和 poll() 方法仅在队列为空时的行为不同:remove() 方法抛出异常,而 poll() 方法返回 null
回答by David Wang
Remove()
method differs from poll only in that it throws an exception if this queue is empty.
Remove()
方法与 poll 的不同之处仅在于,如果此队列为空,它会引发异常。