/ Published in: Java
Expand |
Embed | Plain Text
Comments
Subscribe to comments
You need to login to post a comment.
vonkinder on 05/05/08
1 person have marked this snippet as a favorite
Subscribe to comments
You need to login to post a comment.
To iterate any Map(map, hashMap, etc).
Or, with Generics (replace "Key" and "Value" with the actual types of the key and value) in Java 1.5+:
Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); }
Or, combined with the for-each loop in Java 1.5+:
Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); }
try again [code] Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); } [/code]
[code] Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); } [/code]
Map<Key,Value> mp;
Iterator<Map.Entry<Key,Value>> it = mp.entrySet().iterator();
while (it.hasNext()) {
}
Map<Key,Value> mp;
for (Map.Entry<Key,Value> pair : mp.entrySet()) {
}
Okay, got it. Or, with Generics (replace "Key" and "Value" with the actual types of the key and value) in Java 1.5+: Map mp; Iterator it = mp.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); }
Or, combined with the for-each loop in Java 1.5+: Map mp; for (Map.Entry pair : mp.entrySet()) { System.out.println(pair.getKey() + " = " + pair.getValue()); }