loop over map code example

Example 1: java for map

Map map = new HashMap<>();

for(Entry entry:map.entrySet()) {
  System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
}

Example 2: loop through map in js

const object = {'a': 1, 'b': 2, 'c' : 3};
for (const [key, value] of Object.entries(object)) {
  console.log(key, value);
}

Example 3: java foreach map

map.forEach((k, v) -> System.out.printf( " %s : %d \n" , k,v) );

Example 4: use map to loop through an array

let squares = [1,2,3,4].map(function (val) {  
    return val * val;  
}); 
// squares will be equal to [1, 4, 9, 16]

Tags:

Misc Example