You can iterate over the keys, entries or values.
for (String key : map.keySet()) {
String value = map.get(key);
}
for (String value : map.values()) {
}
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
This is assuming your map has String keys and String values.
Answer from Eran on Stack OverflowYou can iterate over the keys, entries or values.
for (String key : map.keySet()) {
String value = map.get(key);
}
for (String value : map.values()) {
}
for (Map.Entry<String,String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
This is assuming your map has String keys and String values.
You can't directly loop a Map like that in Java.
You can, however, loop the keys:
for (SomeKeyObject key : map.keySet())
The values:
for (SomeValueObject value : map.values())
Or even its entries:
for (Map.Entry<SomeKeyObject, SomeValueObject> entry : map.entrySet())
I have a hashmap where each set's key is an Integer and the value is a custom inner class called node.
My for loop currently looks like this (hm is the variable that holds the hashmap):
for(int x=0; x<hm.entrySet().size(); x++){
HashMap.Entry<Integer, node> currSet = hm.entrySet();
}I know I need to do something to the " hm.entrySet() " inside the for-loop in order to access the set which is at index x. How do I do that?
Ideally I would have liked " hm.entrySet().get(x) " (the same syntax as accessing an arraylist) but that doesn't work.
What is the equivalent of " get(x) " in a situation like this?