how to iterate over map in java 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: java iterate through map

for (Map.Entry entry : map.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    // ...
}

Example 3: iterate through map

//traditional way (long)
for(map::iterator it=m.begin(); it!=m.end(); ++it)
	if(it->second)cout<first<<" ";
//easy way(short) just works with c++11 or later versions
for(auto &x:m)
	if(x.second)cout<

Tags:

Misc Example