获取map集合中key、value

时间:2024-09-17 11:05:20

获取Map集合类中key、value的两种方法

方法一:利用Set集合中的keySet()方法

Map<String,String> map = new HashMap<String,String>();
map.put("name","zhangsan");
map.put("sno","0812");
map.put("cno","3-105"); //将map集合的所有键set到集合中,keySet()
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println("key==="+key+",value==="+value);
}

方法二:利用Set集合的entrySet()方法

Set<Map.Entry<String,String>> es = map.entrySet();
Iterator<String> it = es.iterator(); while(it.hasNext()){
Map.Entry<String,String> mey = it.next();
String key = mey.getKey();
String value = mey.getValue();
System.out.println("key==="+key+",value==="+value);
}  

参考地址:http://blog.****.net/u013506626/article/details/31002795