Iterate Through a Hashmap

Iterate through a HashMap

Iterate through the entrySet() like so:

public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}

Read more about Map.

How do I efficiently iterate over each entry in a Java Map?

Map<String, String> map = ...
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
}

On Java 10+:

for (var entry : map.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
}

How to iterate through a Map in java?

For basic utilisation, the HashMap is the best, I've put how to iterate over it, easier than using an iterator :

public static void main (String[] args) {
//a map with key type : String, value type : String
Map<String,String> mp = new HashMap<String,String>();
mp.put("John","Math"); mp.put("Jack","Math"); map.put("Jeff","History");

//3 differents ways to iterate over the map
for (String key : mp.keySet()){
//iterate over keys
System.out.println(key+" "+mp.get(key));
}

for (String value : mp.values()){
//iterate over values
System.out.println(value);
}

for (Entry<String,String> pair : mp.entrySet()){
//iterate over the pairs
System.out.println(pair.getKey()+" "+pair.getValue());
}
}

A quick explanation :

for (String name : mp.keySet()){
//Do Something
}

means : "For all string from the keys of the map, we'll do something, and at each iteration we will call the key 'name' (it can be whatever you want, it's a variable)


Here we go :

public String[] getAllKeys(){ 
int i = 0;
String allkeys[] = new String[buckets.length];
KeyValue val = buckets[i];

//Look at the first one
if(val != null) {
allkeys[i] = val.key;
i++;
}

//Iterate until there is no next
while(val.next != null){
allkeys[i] = val.next.key;
val = val.next;
i++;
}

return allkeys;
}

Java: Iterate through a HashMap which is inside another HashMap

You could iterate the child map similar to how you've done the parent:

Iterator<Map.Entry<String, Map<String, String>>> parent = PropertyHolder.entrySet().iterator();
while (parent.hasNext()) {
Map.Entry<String, Map<String, String>> parentPair = parent.next();
System.out.println("parentPair.getKey() : " + parentPair.getKey() + " parentPair.getValue() : " + parentPair.getValue());

Iterator<Map.Entry<String, String>> child = (parentPair.getValue()).entrySet().iterator();
while (child.hasNext()) {
Map.Entry childPair = child.next();
System.out.println("childPair.getKey() : " + childPair.getKey() + " childPair.getValue() : " + childPair.getValue());

child.remove(); // avoids a ConcurrentModificationException
}

}

I've presumed you want to call .remove() on the child map, which will lead to a ConcurrentModificationException if done while looping the entrySet - it looks as though you discovered this already.

I've also swapped out your use of casting with strongly-typed generics as suggested in the comments.

Iterating through a HashMap in a For Each style

Use the values collection

for(GameObject gameObject : character.values()) { 
if(gameObject instanceof Stat) { }
}


Related Topics



Leave a reply



Submit