Java 队列中的 add 和 offer 方法有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2703984/
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
What is the difference between the add and offer methods in a Queue in Java?
提问by Finbarr
Take the PriorityQueue
for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)
以http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)PriorityQueue
为例
Can anyone give me an example of a Queue
where the add
and offer
methods are different?
谁能给我的一个例子Queue
,其中add
和offer
方法有什么不同?
According to the Collection
doc, the add
method will often seek to ensure that an element exists within the Collection
rather than adding duplicates. So my question is, what is the difference between the add
and offer
methods?
根据Collection
文档,该add
方法通常会寻求确保元素存在于其中Collection
而不是添加重复项。所以我的问题是,add
和offer
方法之间有什么区别?
Is it that the offer
method will add duplicates regardless? (I doubt that it is because if a Collection
should only have distinct elements this would circumvent that).
该offer
方法是否会添加重复项?(我怀疑这是因为如果 aCollection
应该只有不同的元素,这将绕过它)。
EDIT:
In a PriorityQueue
the add
and offer
methods are the same method (see my answer below). Can anyone give me an example of a class where the add
and offer
methods are different?
编辑:在PriorityQueue
中add
和offer
方法是相同的方法(见我的回答如下)。谁能给我一个add
和offer
方法不同的类的例子?
采纳答案by dvd
I guess the difference is in the contract, that when element can not be added to collection the add
method throws an exception and offer
doesn't.
我想区别在于合同,当元素无法添加到集合时,该add
方法会抛出异常而offer
不会。
From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add%28E%29
来自:http: //java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add%28E%29
If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throwan exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.
如果集合由于任何原因而拒绝添加特定元素,而不是因为它已经包含该元素,则它必须抛出异常(而不是返回 false)。这保留了在此调用返回后集合始终包含指定元素的不变性。
From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html#offer%28E%29
来自:http: //java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html#offer%28E%29
Inserts the specified element into this queue, if possible. When using queues that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception.
如果可能,将指定的元素插入此队列。当使用可能施加插入限制(例如容量限制)的队列时,方法 offer 通常比方法 Collection.add(E) 更可取,后者可能仅通过抛出异常而无法插入元素。
回答by Peter Lang
There is no difference for the implementation of PriorityQueue.add
:
的实现没有区别PriorityQueue.add
:
public boolean add(E e) {
return offer(e);
}
For AbstractQueue
there actually is a difference:
因为AbstractQueue
实际上是有区别的:
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
回答by Stephen C
The difference between offer
and add
is explained by these two excerpts from the javadocs:
这两个摘自 javadoc 的摘录解释了offer
和之间的区别add
:
From the Collection
interface:
从Collection
界面:
If a collection refuses to
add
a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.
如果集合
add
因任何原因拒绝特定元素,但它已包含该元素,则它必须抛出异常(而不是返回 false)。这保留了在此调用返回后集合始终包含指定元素的不变性。
From the Queue
interface
从Queue
界面
When using queues that may impose insertion restrictions (for example capacity bounds), method
offer
is generally preferable to methodCollection.add(E)
, which can fail to insert an element only by throwing an exception.
当使用可能施加插入限制(例如容量限制)的队列时, method
offer
通常比 method 更可取Collection.add(E)
,后者可能仅通过抛出异常而无法插入元素。
PriorityQueue
is a Queue
implementation that does not impose any insertion restrictions. Therefore the add
and offer
methods have the same semantics.
PriorityQueue
是一种Queue
不强加任何插入限制的实现。因此add
和offer
方法具有相同的语义。
By contrast, ArrayBlockingQueue
is an implementation in which offer
and add
behave differently, depending on how the queue was instantiated.
相比之下,ArrayBlockingQueue
是一种实现,其中offer
和add
行为不同,具体取决于队列的实例化方式。
回答by Yeshodhan Kulkarni
Source: http://docs.oracle.com/javase/6/docs/api/java/util/Queue.html
来源:http: //docs.oracle.com/javase/6/docs/api/java/util/Queue.html
The offer method inserts an element if possible, otherwise returning false. This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception. The offer method is designed for use when failure is a normal, rather than exceptional occurrence, for example, in fixed-capacity (or "bounded") queues.
如果可能,offer 方法会插入一个元素,否则返回 false。这与 Collection.add 方法不同,后者只能通过抛出未经检查的异常来添加元素失败。offer 方法设计用于当故障是正常情况而非异常情况时使用,例如,在固定容量(或“有界”)队列中。
回答by Heavin
from the source code in jdk 7 as follow:
来自jdk 7中的源代码如下:
public boolean add(E e) {
if (offer(e))
return true;
else
throw new IllegalStateException("Queue full");
}
we can easily know that the add function will return true when successfully add a new element into the queue, but throw a exception when failed .
我们很容易知道 add 函数在成功向队列中添加新元素时将返回 true,但在失败时抛出异常。
回答by Peter
The Queue
interface specifies that add()
will throw an IllegalStateException
if no space is currently available (and otherwise return true
) while offer()
will return false
if the element couldn't be inserted due to capacity restrictions.
该Queue
接口指定了add()
将抛出IllegalStateException
,如果没有空间是目前可用的(否则返回true
时)offer()
将返回false
如果元素不能由于容量限制插入。
The reason they are the same in a PriorityQueue
is that this queue is specified to be unbounded, i.e. there are no capacity restrictions. In the case of no capacity restrictions, the contracts of add()
and offer()
display the same behaviour.
它们在 a 中相同的原因PriorityQueue
是该队列被指定为无界,即没有容量限制。在没有容量限制的情况下,add()
和的合同offer()
显示相同的行为。
回答by Aslam anwer
I will write the java contract example code for offer method and add method showing how they differ.
我将为 offer 方法和 add 方法编写 java 合同示例代码,显示它们的不同之处。
BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
queue.add("TestQuue1");
queue.add("TestQuue2");
queue.add("TestQuue3"); // will throw "java.lang.IllegalStateException: Queue full
BlockingQueue<String> queue = new ArrayBlockingQueue<>(2);
queue.offer("TestQuue1");
queue.offer("TestQuue2");
queue.offer("TestQuue3"); // will not throw any exception
回答by Maksym Ovsianikov
The difference is following:
区别如下:
offermethod - tries to add an element to a queue, and returns falseif the element can't be added (like in case when a queue is full), or trueif the element was added, and doesn't throw any specific exception.
addmethod - tries to add an element to a queue, returns trueif the element was added, or throws an IllegalStateException if no space is currently available.
提供方法-尝试一个元素添加到队列中,并返回假(当一个队列满等的情况下),或者如果该元件不能被添加真如果加入的元素,并且不会引发任何特定的异常.
add方法 - 尝试将元素添加到队列中,如果添加了元素则返回true,如果当前没有可用空间则抛出 IllegalStateException。