map普通遍历+java8的Lambda表达式

时间:2021-06-28 20:40:37   收藏:0   阅读:0

一:普通方式、

1、keySet():

        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 20);
        for (String key : map.keySet()) {
            String value = map.get(key).toString();
            System.out.println(key);
            System.out.println(value);
        }

2、entrySet():

        Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 20);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        for (Map.Entry<String, Object> entry : entries){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

二:Lambda遍历、

1、entrySet():

 Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 20);
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        entries.forEach(entry -> System.out.println(entry.getKey() + "=" + entry.getValue()));

2、forEach():

Map<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 20);
        map.forEach((key, value) -> System.out.println(key + "=" + value));

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!