Java Queue 接口的 add() 和 offer() 方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20526910/
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
Difference between add() and offer() methods of Queue interface
提问by Aniket Thakur
I was going though FIFO implementation in Java and came across this java.util.Queue interface. Dequeue implements it which in turn is implemented by Linked List.
我正在研究 Java 中的 FIFO 实现并遇到了这个 java.util.Queue 接口。Dequeue 实现它,而它又由 Linked List 实现。
I wrote the following code
我写了以下代码
public class FIFOTest {
public static void main(String args[]){
Queue<String> myQueue = new LinkedList<String>();
myQueue.add("US");
myQueue.offer("Canada");
for(String element : myQueue){
System.out.println("Element : " + element);
}
}
}
Both seem to do the same thing. Add data to the head of the queue. What is the difference between these two methods? Any special cases in which either would be more beneficial than other?
两者似乎都在做同样的事情。将数据添加到队列的头部。这两种方法有什么区别?在任何特殊情况下,哪一个比另一个更有利?
采纳答案by Sotirios Delimanolis
LinkedList#offer(E)
is implemented as
LinkedList#offer(E)
被实现为
public boolean offer(E e) {
return add(e);
}
In this case, they are the same thing. They are just needed to satisfy the interfaces. LinkedList
implements Deque
and List
. The LinkedList#add(E)
method will not throw an Exception
as it will always take more elements, but in another Queue
implementation that has limited capacity or takes only certain kinds of elements, add(E)
might throw an exception while offer(E)
will simply return false
.
在这种情况下,它们是同一回事。它们只需要满足接口。LinkedList
实施Deque
和List
。该LinkedList#add(E)
方法不会抛出 ,Exception
因为它总是需要更多元素,但在另一个Queue
容量有限或仅获取某些类型元素的实现中,add(E)
可能会抛出异常而offer(E)
将简单地返回false
。
回答by Ted Hopp
According to the docsthe main difference is that when the operation fails, one (add
) throws an exception and the other (offer
) returns a special value (false
):
根据文档,主要区别在于当操作失败时,一个 ( add
) 抛出异常,另一个 ( offer
) 返回一个特殊值 ( false
):
Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). The latter form of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.
这些方法中的每一种都以两种形式存在:一种在操作失败时抛出异常,另一种返回一个特殊值(空或假,取决于操作)。后一种形式的插入操作是专门为与容量受限的队列实现一起使用而设计的;在大多数实现中,插入操作不会失败。
回答by Masudul
What is the difference between these two methods?
这两种方法有什么区别?
- Queue.add- throws an exception if the operation fails,
- Queue.offer- returns a special value (either
null
orfalse
, depending on the operation).
- Queue.add- 如果操作失败则抛出异常,
- Queue.offer- 返回一个特殊值(
null
或者false
,取决于操作)。
Any special cases in which either would be more beneficial than other?
在任何特殊情况下,哪一个比另一个更有利?
According to docs, The Queue.offerform of the insert operation is designed specifically for use with capacity-restricted Queue implementations; in most implementations, insert operations cannot fail.
根据文档,插入操作的Queue.offer形式是专门为与容量受限的队列实现一起使用而设计的;在大多数实现中,插入操作不会失败。
For details, read this docs.
有关详细信息,请阅读此文档。
回答by Santosh Joshi
add()
comes fromCollection
Interface.offer()
comes fromQueue
Interface.
add()
来自Collection
接口。offer()
来自Queue
接口。
The Documentation of offer()
method of Queue says
offer()
Queue 方法的文档说
Inserts the specified element into this queue if it is possible to do
so immediately without violating capacity restrictions.
When using a capacity-restricted queue, this method is generally
preferable to {@link #add}, which can fail to insert an element only
by throwing an exception.
The Documentation of add()
method of Queue says
add()
Queue 方法的文档说
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions, returning
<tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
if no space is currently available.