LinkedList 类中的 element() 和 getFirst() 方法有什么区别?
LinkedList
有element()
方法和 getFirst()
方法的描述类似(奇怪的是 - 不是相同的词)。
Deque
清楚声明这两个方法在返回值和异常方面是相同的。
我的问题是 - 为什么有 2 个相同的方法?是为了向后兼容吗?一种方法比另一种方法更有效吗?
LinkedList
has similar descriptions for the element()
method and the getFirst()
method (strangely - not the same words).
Deque
clearly states that the two methods are the same in terms of the return value and the exception.
My question is - why have 2 identical methods? Is it for backward compatibility? Is one approach more efficient than the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
element()
继承自Queue
,其中只有一个 访问方法是有意义的,因为您在队列中所能做的就是删除第一个元素。然而,双端队列从两端都支持这一点,因此需要显式方法来执行此操作。设计一个使用
element()
访问第一个元素并使用getLast()
访问最后一个元素的 API 并不是很好。另一个可能起作用的事情是
Deque
是在 1.6 中添加的,其中 Java 集合框架的部分古老部分已被较新的约定所废弃,例如显式get~/
set~
方法。在这种情况下,getFirst()
和getLast
更严格地遵守当前的 Java 约定。element()
is inherited fromQueue
where it makes sense to have only one accessing method since all you can do in a queue is remove the first element. However, a deque supports this from both ends, necessitating explicit methods to do so.And it's not very nice to design an API where you would access the first element with
element()
and the last withgetLast()
.Another thing that might play into this is that
Deque
was added in 1.6, where parts of the ancient parts of the Java Collections Framework have been obsoleted by newer conventions, such as explicitget~
/set~
methods for property access. In that context,getFirst()
andgetLast
more closely adhere to the current Java conventions.在 Java 1.6 中,
LinkedList
< /a> 实现Deque
< /a>(双端队列)。来自双端队列。 element()
javadocs:在 Java 1.5 中,
LinkedList
有两种方法,但是getFirst()
不受接口支持。我的猜测是,在 Java 1.6 中,他们故意实现了 Deque 来包含此方法。在 Java 1.4 中,
LinkedList
只有getFirst()
,但它不受接口支持。显然我想说这是一个维护向后兼容性的问题:
LinkedList
1.4 有 getFirst() 和只有
List
接口LinkedList
1.5 实现了Queue
因此需要支持等效的elements()
方法LinkedList
1.6 实现Deque
但因为 a) 它必须保持向后兼容,b) 根据策略,所有方法都应该由接口支持,Deque
接口还包括重复方法In Java 1.6,
LinkedList
implementsDeque
(Double-ended Queue). From theDeque.element()
javadocs:In Java 1.5,
LinkedList
has both methods, butgetFirst()
is not backed by an interface. My guess is that in Java 1.6 they implementedDeque
intentionally to include this method.In Java 1.4,
LinkedList
only has thegetFirst()
, but it isn't backed by an interface.Obviously I'd say this is an issue of maintaining backwards compatibility:
LinkedList
1.4 has getFirst() andonly the
List
interfaceLinkedList
1.5 implementsQueue
and hence needs to support the equivalentelements()
methodLinkedList
1.6 implementsDeque
but because a) it has to remain backwards compatible and b) by policy, all methods should be backed by interfaces, theDeque
interface also includes the duplicate method在列出的链接中,看起来这些是相同的。但在队列中,
element()
似乎是一种在队列中的第一个元素处达到峰值的方法,但不会将其从队列中删除。In a link listed it looks like those are the same. But in a Queue,
element()
seems to be a method to peak at the first element in the queue, but not remove it from the queue.