为什么 LinkedHashMap 不提供索引访问?

发布于 2024-11-01 08:15:51 字数 340 浏览 1 评论 0原文

来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卸妝后依然美 2024-11-08 08:15:51

a) 因为条目是链接的,不能随机访问。如果我没有出错的话,性能会很糟糕,O(N)

b) 因为没有接口来支持此功能。因此,选择要么为此(性能不佳)实现引入专用接口,要么要求客户端针对实现类进行编程,而不是


使用 Guava 为您提供了一个简单的解决方案:

Iterables.get(map.values(), offset);

对于缓存,请查看 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:

Iterables.get(map.values(), offset);

And for caching look at Guava's MapMaker and it's expiration features.

别挽留 2024-11-08 08:15:51

由于 values() 提供了值的后备集合,因此您可以像这样解决它:

map.values().remove(map.values().toArray()[index]);

也许不是很有效(尤其是内存方面),但它应该是 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:

map.values().remove(map.values().toArray()[index]);

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 than LinkedList anyway, right?)

I set out to do a LinkedHashMapList which extended the LinkedHashMap and implemented the List interface. Surprisingly it seems impossible to do, due to the clash for remove. The existing remove method returns the previously mapped object, while the List.remove should return a boolean.

That's just a reflection, and honestly, I also find it annoying that the LinkedHashMap can't be treated more like a LinkedList.

余厌 2024-11-08 08:15:51

它提供了一个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 a get(i) method would be no different than iterating over all the elements in the list since there is no backing array (same as LinkedList).

If you require this ability which isn't very performant I suggest extending the map yourself

給妳壹絲溫柔 2024-11-08 08:15:51

如果你想要随机访问,你可以这样做

Map<K,V> map = new LinkedHashMap<K,V>();
Map.Entry<K,V>[] entries = (Map.Entry<K,V>[]) map.toArray(new Map.Entry[map.size()]);
Map.Entry<K,V> entry_n = entry[n];

正如你所看到的,除非你缓存 entries 数组,否则性能可能会很差。

但我会质疑这样做的必要性。

If you want random access you can do

Map<K,V> map = new LinkedHashMap<K,V>();
Map.Entry<K,V>[] entries = (Map.Entry<K,V>[]) map.toArray(new Map.Entry[map.size()]);
Map.Entry<K,V> entry_n = entry[n];

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.

甜妞爱困 2024-11-08 08:15:51

制作一个索引访问效率为 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文