您的位置:首页 > 编程语言 > Java开发

Java——Iterate through a HashMap

2016-04-06 08:34 435 查看
遍历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 [code]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
}

}
}[/code]

来源:http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: