Java LinkedList - 检索操作之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14851367/
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
Java LinkedList - differences between retrieve operations
提问by Elist
Are there any differences between different methods in each of the following groups of element retrieve operations in LinkedList
?
在以下各组元素检索操作中,不同方法之间是否有任何区别LinkedList
?
Returning null + removing operations:poll()
, pollFirst()
.
返回 null + 删除操作:poll()
, pollFirst()
。
Returning null + not removing operations:peek()
, peekFirst()
.
返回 null + 不删除操作:peek()
, peekFirst()
。
Throwing exception + removing operations:pop()
, remove()
, removeFirst()
.
抛出异常 + 移除操作:pop()
, remove()
, removeFirst()
.
Throwing exception + not removing operations:element()
, getFirst()
.
抛出异常 + 不删除操作:element()
, getFirst()
.
Similar duplications exists in insertion methods.
插入方法中也存在类似的重复。
If there is no such difference, I would expect it to be mentioned in the javadoc of the methods (something like the good old "This is exactly like calling ..."). Is it only a sloppy documentation, or am I missing anything?
如果没有这样的区别,我希望在方法的 javadoc 中提到它(类似于古老的“这就像调用......”一样)。它只是一个草率的文档,还是我遗漏了什么?
采纳答案by Rohit Jain
There is no difference between them, and it is listed in the documentation too, but you have to do some recursive searching to get there.
它们之间没有区别,文档中也列出了它,但是您必须进行一些递归搜索才能到达那里。
LinkedList
implements two interfaces - Queue
and Deque
. And Deque
extends from Queue
.
LinkedList
实现两个接口 -Queue
和Deque
. 并Deque
从Queue
.
Now, Deque
has defined the method - Deque#pollFirst()
and inherited the method - Queue#poll()
.
现在,Deque
已经定义了方法——Deque#pollFirst()
并继承了方法—— Queue#poll()
。
So, LinkedList
has basically these two methods defined for the two interfaces it implements.
因此,LinkedList
基本上为它实现的两个接口定义了这两个方法。
And about the similarity between those two methods, it is listed in documentation of Deque
as:
关于这两种方法之间的相似性,它在以下文档中列出Deque
:
This interface extends the Queue interface. When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. Elements are added at the end of the deque and removed from the beginning. The methods inherited from the Queue interface are precisely equivalent to Deque methods as indicated in the following table:
该接口扩展了 Queue 接口。当双端队列用作队列时,会产生 FIFO(先进先出)行为。元素添加在双端队列的末尾并从开头删除。从 Queue 接口继承的方法与 Deque 方法完全等效,如下表所示:
And there is a table listing the methods of Queue
class and the equivalent Deque
method. See Deque#poll()
, Deque#peek()
for e.g. They clearly list the equivalent method.
并且有一个表格列出了Queue
类的方法和等效Deque
方法。参见Deque#poll()
,Deque#peek()
例如他们清楚地列出了等效的方法。
回答by user3659225
Your right it's bad documentation or something.
你的权利是糟糕的文档或其他东西。
peek() Retrieves, but does not remove, the head (first element) of this list.
peek() 检索但不删除此列表的头部(第一个元素)。
peekFirst() Retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
peekFirst() 检索但不删除此列表的第一个元素,如果此列表为空,则返回 null。
This is what it says and I'm read here in my Java book that the head is the first item in a list.
这就是它所说的,我在我的 Java 书中读到,头部是列表中的第一项。
回答by BartoszMiller
The difference between them is version they were released with and the interfaces that LinkedList implements.
它们之间的区别在于它们发布的版本以及 LinkedList 实现的接口。
Example based on poll()
and pollFirst()
:
基于poll()
和的示例pollFirst()
:
LinkedList
was released along with Java 1.2.
LinkedList
与Java 1.2一起发布。
Since 1.5LinkedList implements the Queue interface, which has
由于 1.5LinkedList 实现了 Queue 接口,它具有
public E poll()
Since 1.6LinkedList implements the Deque interface, which has
由于 1.6LinkedList 实现了 Deque 接口,它具有
public E pollFirst()
edit:
It is important to keep the older implementation due to a backward compatibility.
编辑:
由于向后兼容性,保留旧的实现很重要。