java 迭代以在索引处查找 Map 条目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3895525/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:53:12  来源:igfitidea点击:

Iterate to find a Map entry at an index?

java

提问by user291701

I have a LinkedHashMap. I want to get the Foo at index N. Is there a better way of doing this besides iterating until I find it?:

我有一个 LinkedHashMap。我想在索引 N 处获取 Foo。除了迭代直到找到它之外,还有更好的方法吗?:

int target = N;
int index = 0;
for (Map.Entry<String, Foo> it : foos.entrySet()) {
    if (index == target) {
        return it.getValue();
    }
    index++;
}

I have to do get random elements from the map by an index about 50 times for some operation. The map will have about 20 items in it.

对于某些操作,我必须通过大约 50 次的索引从地图中获取随机元素。该地图将包含大约 20 个项目。

Thanks

谢谢

回答by Mark Elliot

List<Entry<String,Foo>> randAccess = new ArrayList<Entry<String,Foo>>(foos.entrySet());

Then for index N with O(1) access...

然后对于具有 O(1) 访问权限的索引 N...

randAccess.get(N)

回答by Stephen C

@Mark's solution is spot on. I'd just like to point out that the offsets (positions) of the entries in a map (of any kind) are not stable. Each time an entry is added or removed, the offsets of the remaining entries maychange. For a HashMap or LinkedHashMap, you've no way of knowing which entry's offsets will change.

@Mark 的解决方案恰到好处。我只想指出地图(任何类型)中条目的偏移量(位置)并不稳定。每次添加或删除条目时,剩余条目的偏移量可能会发生变化。对于 HashMap 或 LinkedHashMap,您无​​法知道哪个条目的偏移量会发生变化。

  • For a regular HashMap, a single insertion can apparently "randomize" the entry offsets.
  • For a LinkedHashMap, the order of the entries is stable, the actual entry offsets are not.
  • 对于常规 HashMap,单个插入显然可以“随机化”条目偏移量。
  • 对于 LinkedHashMap,条目的顺序是稳定的,实际的条目偏移量不是。

The instability of the offsets and the fact that finding entry at a given offset is expensive for all standard map implementations are the reasons why the Mapinterface does not provide a get(int offset)method. It should also be a hint that it is not a good idea for an algorithm to need to do this sort of thing.

偏移量的不稳定性以及在给定偏移量处查找条目对于所有标准映射实现来说都是昂贵的这一事实是Map接口不提供get(int offset)方法的原因。这也应该是一个暗示,算法需要做这种事情并不是一个好主意。

回答by Peter Lawrey

A simplification of @Mark's solution... You only need the values, so each time you change a value in the foos Map, also update an array.

@Mark 解决方案的简化...您只需要值,因此每次更改 foos Map 中的值时,也要更新一个数组。

Map<String, Foo> foos =;
Foo[] fooValues = {};

foos.put(foos.name(), foo);
fooValues = foos.values().toArray(new Foo[foos.size()]);

// later
Foo foo = fooValues[N];

回答by Alex K

Guava library can help in this case:

在这种情况下,番石榴库可以提供帮助:

public static <T> T com.google.common.collect.Iterables.get(Iterable<T> iterable, int position)

see javadoc: Iterables.get

请参阅 javadoc:Iterables.get

For your case the code can be like this:

对于您的情况,代码可以是这样的:

Iterables.get(foos.values(), N);