loop over map 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: foreach map java

Map map = ...
for (Map.Entry entry : map.entrySet()) {
    System.out.println(entry.getKey() + "/" + 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