码迷,mamicode.com
首页 > 编程语言 > 详细

java_HashMap的遍历方法_4种

时间:2020-02-08 22:01:43      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:就是   帮助   style   tor   map   reac   new   entry   print   

1.通过接收keySet来遍历:

        HashMap<String,String> map = new HashMap<>();
        map.put("bb","12");
        map.put("aa","13");
        for(String each:map.keySet()){
            System.out.println("key:"+each+"value:"+map.get(each));
        }    

 

输出为:

技术图片

 

 2,通过entrySet来遍历

 

for(Map.Entry<String,String> each:map.entrySet()){
            System.out.println("key:"+each.getKey()+" value:"+each.getValue());
        }

 

输出为:

技术图片

 

 3.使用迭代器遍历(实质上foreach语句就是迭代器实现)

Iterator test01 = map.entrySet().iterator();
        while(test01.hasNext()){
            System.out.println(test01.next());
        }

 

输出为:

技术图片

 

 

4.通过valueSet来遍历(只能遍历到值,但是hashMap是加入有序的,所以不用担心和加入顺序不一样)

for(String each:map.values()){
            System.out.println(each);
        }

 

输出为:

技术图片

 

 

 

希望对大家有所帮助

以上

java_HashMap的遍历方法_4种

标签:就是   帮助   style   tor   map   reac   new   entry   print   

原文地址:https://www.cnblogs.com/lavender-pansy/p/12285336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!