遍历Map
import java.util.*;
public class IterateHashMap {
public static void main(String[] args) {
Map<String,Object> map=new HashMap<String,Object>();
// If you're only interested in the keys, you can iterate through the keySet()
of the map:
for (String key : map.keySet())
{
// ...
}
//If you only need the values, use values():
for (Object value : map.values())
{
// ...
}
//Finally, if you want both the key and value, use entrySet():
for (Map.Entry<String, Object> entry : map.entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
// ...
}
//
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove(); // avoids a ConcurrentModificationException
}
}
}
来源:http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap