获取 LinkedHashMap 中最后一个键或值的最便捷方法?
LinkedHashMap
描述说“它维护一个贯穿其所有条目的双向链表”,所以我想知道如何获取最后输入的条目或键?我可以自信地将 .values()
向下转换为 LinkedList
来获取双向链表并使用 .getLast()
吗?或者它是某个其他 Java 集合的实例?
如果可能的话,我想坚持使用 java.util。
LinkedHashMap
description says "it maintains a doubly-linked list running through all of its entries" so I'm wondering how to get the last entry or key entered? Can I confidently downcast .values()
to LinkedList
to get that doubly-linked list and use .getLast()
of that? Or is it an instance of some other Java collection?
I want to stick with java.util
if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,你可以获得最后一个元素。但您必须查看其他人的建议才能获取
values()
返回的Collection
的最后一个元素。我检查了源代码,返回的值确实是按照预期的顺序:
LinkedListMap.values()
返回的AbstactCollection
由一个Iterator
支持,其值本身直接链接到键上的Iterator
。显然,键上的Iterator
是通过有序双向链表实现的。Yes, you can get the last element. But you'll have to look at the suggestions from others to get the last element of the
Collection<V>
returned byvalues()
.I checked in the source code that the returned values are indeed in the expected order:
The
AbstactCollection<V>
returned byLinkedListMap.values()
is backed by anIterator<V>
over the values which is itself directly linked to theIterator<K>
over the keys. And obviously theIterator<K>
over the keys is implemented with the ordered doubly linked list.不,抱歉,你不能。
“维护的双向链表”不是任何 java.util.LinkedList 类型或其他集合。它是在 LinkedHashMap 和 LinkedHashMap.Entry 类中手动实现的。
您只能从
values()
构建LinkedList
,然后使用letLast()
:No, sorry, you cannot.
The "maintained doubly-linked list" is not any java.util.LinkedList type or other collection. It's manually implemented in LinkedHashMap and LinkedHashMap.Entry classes.
You can only build
LinkedList
fromvalues()
and then useletLast()
:更新:我之前的回答是错误的。如果不修改默认行为,您就无法做到这一点!请参阅下面的原因。
从
LinkedHashMap
的 API 描述中您可以看到:那么这一切意味着什么呢?
put
或get
时,元素的顺序都会发生变化例如:
...将打印
{1=10, 2=2} 与 插入顺序 和
{2=2, 1=10}
带有 *access-ordered'。问题在于使用access-ordered
,当然,如果您执行get
操作,顺序也会改变。如何修复
那么...如何修复。那么
LinkedHashMap
不能直接使用。因此,您可以包装它(不要关心老生常谈的名称)并覆盖put
和putAll
方法,以便它们先从地图中删除密钥,然后再将其放回去在!然后要获取最后一个元素,可以执行以下操作之一:
将输出包装在
LinkedList
实现中:toArray()
方式:使用迭代器迭代到最后一个元素:
Update: My previous answer was wrong. You cannot do it without modifying the default behaviour! See below why.
From the API description of
LinkedHashMap
you can read:So what does it all mean?
put
or aget
the order of the elements changesFor example:
... will print
{1=10, 2=2}
with insertion-ordered and{2=2, 1=10}
with *access-ordered'. The trouble is usingaccess-ordered
if of course if you do aget
operations the order also changes.How to fix
So... how to fix. Well the
LinkedHashMap
cannot be used directly used. So you can wrap it (do not care about the corny name) and override theput
and theputAll
methods so that they remove the key from the map first before putting it back in!Then to get the last element, either do:
wrap the output from in a
LinkedList
implementation:toArray()
way:iterate to the last element using the iterator:
我已经“扩展”了Jdk LinkedHashMap以允许这样做,你可以看一下: LinkedHashMapEx.java
I have "extended" the Jdk LinkedHashMap to allow that, you can take a look at: LinkedHashMapEx.java