From the third edition of the Java specification, here's an example of how you use Java 1.5's foreach construct with a Map. That's:
for(Map.Entry<K, V> e : map.entrySet())
System.out.println(e.getKey()+": "+e.getValue());
Of course, to iterate just over the values:
for(V e : map.values())
System.out.println(e);
To do the same thing with the keys, use keySet() instead of values();
Update (7/22): For reference, here's a way to iterate over a map that doesn't assume generics or the types of the map's key:values:
public static void dumpMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
System.out.println(pairs.getKey() + " = " + pairs.getValue());
}
}
new⇒I hate ASP.NET
CF, why pick that piece of trash?Cold Confusion. Is it finallyreally a OO...
ColdConfusion: Sep 5, 8:36pm