Java - 在 HashMap 中获取键的索引?

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

Java - get index of key in HashMap?

javaiterationhashmap

提问by llm

In java if I am looping over the keySet()of a HashMap, how do I (inside the loop), get the numerical index of that key?

在java中,如果我循环过keySet()HashMap,我怎么(内循环),拿到钥匙的数值指标?

Basically, as I loop through the map, I want to be able to get 0,1,2...I figure this would be cleaner than declaring an int and incrementing with each iteration.

基本上,当我遍历地图时,我希望能够得到 0,1,2...我认为这比声明一个 int 并随着每次迭代递增更清晰。

Thanks.

谢谢。

回答by Esko

Simply put, hash-based collections aren't indexed so you have to do it manually.

简而言之,基于散列的集合没有索引,因此您必须手动进行。

回答by Matthew Flaschen

The HashMap has no defined ordering of keys.

HashMap 没有定义的键顺序。

回答by Kris Jenkins

You can't - a set is unordered, so there's no index provided. You'll have to declare an int, as you say. Just remember that the next time you call keySet() you won't necessarily get the results in the same order.

你不能 - 集合是无序的,所以没有提供索引。正如你所说,你必须声明一个int。请记住,下次您调用 keySet() 时,您不一定会以相同的顺序获得结果。

回答by Binil Thomas

Not sure if this is any "cleaner", but:

不确定这是否是“更清洁”,但是:

List keys = new ArrayList(map.keySet());
for (int i = 0; i < keys.size(); i++) {
    Object obj = keys.get(i);
    // do stuff here
}

回答by aperkins

If all you are trying to do is get the value out of the hashmap itself, you can do something like the following:

如果您要做的只是从哈希映射本身中获取值,则可以执行以下操作:

for (Object key : map.keySet()) {
    Object value = map.get(key);
    //TODO: this
}

Or, you can iterate over the entries of a map, if that is what you are interested in:

或者,您可以遍历地图的条目,如果这是您感兴趣的:

for (Map.Entry<Object, Object> entry : map.entrySet()) {
    Object key = entry.getKey();
    Object value = entry.getValue();
    //TODO: other cool stuff
}

As a community, we might be able to give you better/more appropriate answers if we had some idea why you needed the indexes or what you thought the indexes could do for you.

作为一个社区,如果我们知道您为什么需要索引或您认为索引可以为您做什么,我们可能会为您提供更好/更合适的答案。

回答by trayzey

Use LinkedHashMap instead of HashMap It will always return keys in same order (as insertion) when calling keySet()

使用 LinkedHashMap 而不是 HashMap 当调用 keySet() 时,它总是会以相同的顺序返回键(作为插入)

For more detail, see Class LinkedHashMap

有关更多详细信息,请参阅类 LinkedHashMap

回答by Neelesh Salian

I was recently learning the concepts behind Hashmap and it was clear that there was no definite ordering of the keys. To iterate you can use:

我最近正在学习 Hashmap 背后的概念,很明显键没有明确的排序。要迭代,您可以使用:

Hashmap<String,Integer> hs=new Hashmap();
for(Map.Entry<String, Integer> entry : hs.entrySet()){
      String key=entry.getKey();
      int val=entry.getValue();
      //your code block  
  }

回答by Benjineer

Posting this as an equally viable alternative to @Binil Thomas's answer - tried to add it as a comment, but was not convinced of the readability of it all.

将此作为@Binil Thomas 答案的同样可行的替代方案发布 - 试图将其添加为评论,但不相信这一切的可读性。

int index = 0;

for (Object key : map.keySet()) {
   Object value = map.get(key);
   ++index;
}

Probably doesn't help the original question poster since this is the literal situation they were trying to avoid, but may aid others searching for an easy answer.

可能对原始问题发布者没有帮助,因为这是他们试图避免的字面情况,但可能有助于其他人寻找简单的答案。