首页 > 其他 > 详细

遍历Map的两种方法

时间:2017-01-21 09:54:28      阅读:215      评论:0      收藏:0      [点我收藏+]

MAP集合遍历的两种方法

1、使用keyset()获得Map中的的key ,然后使用get方法获得这个key对应的value;

示例:Map<String,Integer> map = new HashMap<String,Integer>();

        map.put("张三",15);

        map.put("李四",16);

        map.put("王五",17);

        Set ss = map.keyset();

        Iterator<String> it = new ss.iterator();

        while (it.Hashnext()){

             String str = it.next();

             int va = str.get();

             System.out.println(str + va);    

         }

2、使用entrySet获得Map.Entry<K,V>类型,然后Entry有自己的getkey、getvalue方法

示例:

Map<String,Integer> map = new HashMap<String,Integer>();

        map.put("张三",15);

        map.put("李四",16);

        map.put("王五",17);

        Set<Map.Entry<String,Integer>> ss = map.entrySet();

        Iterator<Map.Entry<String,Integer>> it = new ss.iterator();

        while (it.Hashnext()){

             Map.Entry<String,Integer> str = it.next();

             String key = str.getkey();

             int va = str.getvalue();

             System.out.println(key + va);    

         }

 

遍历Map的两种方法

原文:http://www.cnblogs.com/ailsalin/p/6322564.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!