简体   繁体   English

Java - 获取 HashMap 中的键索引?

[英]Java - get index of key in HashMap?

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 中,如果我在HashMapkeySet()上循环,我如何(在循环内)获取该键的数字索引?

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.基本上,当我循环遍历 map 时,我希望能够获得 0,1,2...我认为这比声明一个 int 并在每次迭代中递增更清晰。

Thanks.谢谢。

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

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
}

HashMap 没有定义的键顺序。

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

You can't - a set is unordered, so there's no index provided.你不能 - 集合是无序的,所以没有提供索引。 You'll have to declare an int, as you say.正如你所说,你必须声明一个int。 Just remember that the next time you call keySet() you won't necessarily get the results in the same order.请记住,下次您调用 keySet() 时,您不一定会以相同的顺序获得结果。

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

I was recently learning the concepts behind Hashmap and it was clear that there was no definite ordering of the keys.我最近正在学习 Hashmap 背后的概念,很明显键没有明确的顺序。 To iterate you can use:要迭代,您可以使用:

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  
  }

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

You can directly get the keys in Set<K> keySet() .您可以直接在Set<K> keySet()中获取密钥。

val teamMember = hashMapOf(
            "Coach" to "manager",
            "Player Coach" to "editor",
            "Player" to "member",
            "Supporter" to "readonly"
        )

teamMember.keys.forEachIndexed { index, key ->
   // Here is your key in string 
}

I don't know is this what you're looking for我不知道这是你要找的

List keys = new ArrayList(map.keySet());
int index = keys.indexOf(element);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM