为什么 LinkedHashMap 不提供索引访问?
来自 Javadoc:Map 接口的哈希表和链表实现,具有可预测的迭代顺序。此实现与 HashMap 的不同之处在于,它维护一个贯穿其所有条目的双向链表。
如果是这样,那么为什么它不提供像 java 中的 List 那样的对象访问, 列表.get(索引);
更新
我使用 LinkedHashMap 实现了 LRU 缓存。我的算法要求我从缓存中访问 LRU 对象。这就是为什么我需要随机访问,但我认为这会降低我的性能,所以我改变了逻辑,我在缓存已满时访问LRU对象...使用removeEldestEntry()
谢谢大家...
From Javadoc:Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries.
If it is so, then why doesn't it provide object access like List in java,
list.get(index);
UPDATE
I had implemented LRU Cache using LinkedHashMap. My algorithm required me to access LRU Object from the cache. That's why I required random access, but I think that will cost me bad performance, so I have changed the logic and I am accessing the LRU object just when Cache is full...using removeEldestEntry()
Thank you all...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
a) 因为条目是链接的,不能随机访问。如果我没有出错的话,性能会很糟糕,
O(N)
。b) 因为没有接口来支持此功能。因此,选择要么为此(性能不佳)实现引入专用接口,要么要求客户端针对实现类进行编程,而不是
使用 Guava 为您提供了一个简单的解决方案:
对于缓存,请查看 Guava 的
MapMaker
及其过期功能。a) Because the entries are linked, not randomly accessible. The performance would be miserable,
O(N)
if I'm not in error.b) Because there is no interface to back up this functionality. So the choice would be to either introduce a dedicated interface just for this (badly performing) Implementation or require clients to program against implementation classes instead of interfaces
Btw with Guava there's a simple solution for you:
And for caching look at Guava's
MapMaker
and it's expiration features.由于
values()
提供了值的后备集合,因此您可以像这样解决它:也许不是很有效(尤其是内存方面),但它应该是
O(N)
code> 正如您所期望的那样。顺便说一句,我认为这个问题对于所有
List
操作都是合法的。 (无论如何,它不应该比LinkedList
慢,对吧?)我开始做一个
LinkedHashMapList
,它扩展了LinkedHashMap
并实现了 <代码>列表接口。令人惊讶的是,由于删除冲突,这似乎不可能做到。现有的remove
方法返回先前映射的对象,而List.remove
应返回一个boolean
。这只是一种反映,老实说,我还发现不能将
LinkedHashMap
视为更像LinkedList
,这很烦人。Since
values()
provides a backing collection of the values, you can solve it like this:Perhaps not very efficient (especially memory-wise), but it should be
O(N)
just as you would expect it to be.Btw, I think the question is legitimate for all
List
operations. (It shouldn't be slower thanLinkedList
anyway, right?)I set out to do a
LinkedHashMapList
which extended theLinkedHashMap
and implemented theList
interface. Surprisingly it seems impossible to do, due to the clash for remove. The existingremove
method returns the previously mapped object, while theList.remove
should return aboolean
.That's just a reflection, and honestly, I also find it annoying that the
LinkedHashMap
can't be treated more like aLinkedList
.它提供了一个Iterator接口,列表中的每个节点都链接到它之前和之后的节点。使用
get(i)
方法与迭代列表中的所有元素没有什么不同,因为没有后备数组(与LinkedList
相同)。如果您需要这种性能不是很好的功能,我建议您自己扩展地图
It provides an
Iterator
interface, each node in the list is linked to the one before it and after it. Having aget(i)
method would be no different than iterating over all the elements in the list since there is no backing array (same asLinkedList
).If you require this ability which isn't very performant I suggest extending the map yourself
如果你想要随机访问,你可以这样做
正如你所看到的,除非你缓存
entries
数组,否则性能可能会很差。但我会质疑这样做的必要性。
If you want random access you can do
As you can see the performance is likely to be very poor unless you cache the
entries
array.I would question the need for it however.
制作一个索引访问效率为 log(N) 的 Map 并没有什么实际问题。如果您使用红黑树并为每个节点存储从该节点开始的树中的元素数量,则可以编写一个 get(int index) 方法,即 log(N)。
There is no real problem to make a Map with log(N) efficiency of access by index. If you use a red-black tree and store for each node the number of elements in the tree starting at that node it is possible to write a get(int index) method that is log(N).