獲取map集合中key、value


獲取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.csdn.net/u013506626/article/details/31002795


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM