Java Queue 中的 add 和 Offer 方法有什么区别?
以 PriorityQueue
为例 http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)
谁能给我一个例子队列
其中添加
和 offer
方法有什么不同?
根据集合
doc,add
方法通常会寻求确保元素存在于 Collection
中,而不是添加重复项。所以我的问题是,add
和 offer
方法有什么区别?
offer
方法是否会添加重复项? (我怀疑这是因为如果 Collection
应该只包含不同的元素,这就会规避这一点)。
编辑: 在 PriorityQueue
中,add
和 offer
方法是相同的方法(请参阅下面的我的答案)。谁能给我一个 add
和 offer
方法不同的类的示例?
Take the PriorityQueue
for example http://java.sun.com/j2se/1.5.0/docs/api/java/util/PriorityQueue.html#offer(E)
Can anyone give me an example of a Queue
where the add
and offer
methods are different?
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?
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).
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
到队列:
add(E e)
和offer(E e)
。如果可以立即执行此操作而不违反容量限制,这两种方法都会将指定元素添加到队列中。但是,行为上有所不同:
add(E e):如果由于容量限制此时无法添加元素,则此方法会抛出
IllegalStateException
。offer(E e):如果由于容量限制无法添加元素,该方法返回
false
,否则添加元素并返回true
。下面是一个小例子来说明差异:
输出:
将元素 1 到 3 添加到队列中。
队列达到其容量。
而 offer() 方法在这种情况下返回 false。
to the queue:
add(E e)
andoffer(E e)
.Both methods add the specified element to the queue if it is possible to do so immediately without violating capacity restrictions. However, there is a difference in behavior:
add(E e): This method throws an
IllegalStateException
if the element cannot be added at this time due to capacity restrictions.offer(E e): This method returns
false
if the element cannot be added due to capacity restrictions, otherwise it adds the element and returnstrue
.Here's a small example to illustrate the difference:
Output :
adds elements 1 to 3 to the queue.
the queue reaches its capacity.
while the offer() method returns false in such cases.
如果添加完成,offer 方法抛出 true 或 false
add 方法在队列中无法添加时抛出异常
offer method throws true or false, if addition is done
add method throws an exception when no addition possible in queue
来源: http://docs.oracle.com/javase /6/docs/api/java/util/Queue.html
Offer 方法会在可能的情况下插入一个元素,否则返回 false。这与 Collection.add 方法不同,后者只能通过抛出未经检查的异常来添加元素失败。 Offer 方法设计用于当故障是正常现象而不是异常情况时使用,例如在固定容量(或“有界”)队列中。
Source: 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.
从jdk 7中的源代码中
我们可以很容易地知道,add函数在向队列中添加新元素成功时会返回true,而在失败时会抛出异常。
from the source code in jdk 7 as follow:
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 .
我将为
offer
方法和add
方法编写 java 合约示例代码,展示它们的不同之处。I will write the java contract example code for the
offer
method and theadd
method showing how they differ.Queue
接口指定,如果当前没有可用空间,add()
将抛出IllegalStateException
(否则返回true) 而如果由于容量限制而无法插入元素,
offer()
将返回false
。它们在
PriorityQueue
中相同的原因是该队列被指定为无界的,即没有容量限制。在没有容量限制的情况下,add()
和offer()
合约显示相同的行为。The
Queue
interface specifies thatadd()
will throw anIllegalStateException
if no space is currently available (and otherwise returntrue
) whileoffer()
will returnfalse
if the element couldn't be inserted due to capacity restrictions.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 ofadd()
andoffer()
display the same behaviour.javadoc 中的以下两个摘录解释了
offer
和add
之间的区别:来自
集合
接口:来自
队列
接口PriorityQueue
是一个Queue
实现,它不施加任何插入限制。因此,add
和offer
方法具有相同的语义。相比之下,ArrayBlockingQueue 是一种实现,其中
offer
和add
的行为有所不同,具体取决于队列的实例化方式。The difference between
offer
andadd
is explained by these two excerpts from the javadocs:From the
Collection
interface:From the
Queue
interfacePriorityQueue
is aQueue
implementation that does not impose any insertion restrictions. Therefore theadd
andoffer
methods have the same semantics.By contrast,
ArrayBlockingQueue
is an implementation in whichoffer
andadd
behave differently, depending on how the queue was instantiated.区别如下:
offer 方法 - 尝试将元素添加到队列中,如果元素无法添加,则返回 false被添加(例如队列已满的情况),或者 true 如果元素已添加,并且不会抛出任何特定异常。
add 方法 - 尝试将元素添加到队列中,如果添加了元素,则返回 true;如果没有添加,则抛出 IllegalStateException空间当前可用。
The difference is following:
offer method - tries to add an element to a queue, and returns false if the element can't be added (like in case when a queue is full), or true if the element was added, and doesn't throw any specific exception.
add method - tries to add an element to a queue, returns true if the element was added, or throws an IllegalStateException if no space is currently available.
简短的回答:这取决于具体实施。
如果您的队列有最大容量限制,则实际上存在差异。
与
PriorityQueue
:实际上存在差异,例如
ArrayBlockingQueue
扩展了AbstractQueue
:offer
:如果队列已满,则返回false
并且将不抛出异常add
:如果队列已满,则抛出异常The short answer: it depends on the concrete implementation.
If your queue has a max capacity limit, there actually is a difference.
There is no difference like the implementation of
PriorityQueue
:There actually is a difference like the implementation of
ArrayBlockingQueue
which extendsAbstractQueue
:offer
: if the queue is full, returnfalse
and will not throw an exceptionadd
: if the queue is full, throw an exception我想区别在于合同,当元素无法添加到集合中时,
add
方法会抛出异常,而offer
则不会。来自: 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/Queue.html#offer%28E%29
I guess the difference is in the contract, that when element can not be added to collection the
add
method throws an exception andoffer
doesn't.From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collection.html#add%28E%29
From: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Queue.html#offer%28E%29