标签:通过 sys nbsp keyset key 键值 str val ring
遍历map集合有三种:
1.通过map.keySet()获取key的Set集合;
2.通过map.values()获取所有的value的Collection集合;
3.通过map.entrySet()获取所有的Set键值对集合;
获取key,value的集合后通过迭代器Iterator遍历;
实例代码:
Map<String,Object> map = new HashMap();
map.put("1",1);
map.put("2",‘s‘);
map.put("3","z");
map.put("4",true);
map.put(null,"0");
/**
* Map.keyset 获取key的集合
*/
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()){
String next = iterator.next();
Object o = map.get(next);
System.out.println(o);
}
/**
* 获取value的集合
*/
Collection<Object> values = map.values();
Iterator<Object> it = values.iterator();
while (it.hasNext()){
Object next = it.next();
System.out.println(next);
}
/**
* 获取key-value键值对集合
*/
Set<Map.Entry<String, Object>> entries = map.entrySet();
Iterator<Map.Entry<String, Object>> ite = entries.iterator();
while (ite.hasNext()){
Map.Entry<String, Object> next = ite.next();
System.out.println(next.getKey()+": "+next.getValue());
}
标签:通过 sys nbsp keyset key 键值 str val ring
原文地址:https://www.cnblogs.com/xileman/p/11321002.html