50

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?

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.

Thanks.

llm
  • 5,539
  • 12
  • 36
  • 30
  • 3
    What do you need it for? As mentioned in the answers, order in a map is not necessarily constant, it can change when keys get added or removed. – extraneon Apr 21 '10 at 19:54
  • 6
    What's even better is that two HashMaps that are equal can still have different orderings. They can do it *even if they have the same history of adds/removes* (different capacities would do it). – Donal Fellows Apr 21 '10 at 20:08
  • http://stackoverflow.com/questions/18188739/arraylist-of-hashmap-or-linkedhashmap-to-get-item-by-index/18959271#18959271 – Vinay W Sep 23 '13 at 12:36

10 Answers10

59

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

For more detail, see Class LinkedHashMap

MM.
  • 2,653
  • 4
  • 26
  • 36
trayzey
  • 591
  • 4
  • 2
49

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
}
Binil Thomas
  • 13,699
  • 10
  • 57
  • 70
  • 5
    Or similarly: `int index = 0; for (Object key : map.keySet()) { `Object value = map.get(key); ++index; } // dang formatting – Benjineer Dec 01 '15 at 09:40
21

The HashMap has no defined ordering of keys.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
14

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.

aperkins
  • 12,914
  • 4
  • 29
  • 34
9

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.

Kris Jenkins
  • 4,083
  • 30
  • 40
  • 1
    If the OP does really *want* this, it is not hard to write a collection backed by a TreeSet and a Map. That way you can get the results always in the same order. Now as to how to behave when the Map is modified during iteration is up to the OP but a data structure that is a map where the keys are in an ordered set is definitely doable. (btw I'm more commenting on your answer than on the OP's question). – SyntaxT3rr0r Apr 21 '10 at 20:07
  • This answer is wrong. Whilst it is true that a set per se is unsorted a LinkedHashMap does indeed keeps the insertion order so on iteration it is possible to get it index based – Niton Aug 15 '20 at 14:47
5

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

Esko
  • 29,022
  • 11
  • 55
  • 82
4

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.

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.

Benjineer
  • 1,530
  • 18
  • 22
3

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

I don't know is this what you're looking for

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

You can directly get the keys in 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 
}
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60