java8 list和map的forEach

 

public static void main(String[] args) {
        List<User> list = new ArrayList<>();
        for(int i=1;i<=10;i++) {
            list.add(new User(i, "user_" + i));
        }
        //list forEach
        list.forEach(user -> {
            user.setId(user.getId() + 10);
            System.out.println(user);
        });

    }
public static void main(String[] args) {
        Map<Integer, User> map = new HashMap<>(10);
        for(int i=1;i<=10;i++) {
            map.put(i, new User(i, "user_" + i));
        }
        //map forEach
        map.forEach((k, v) -> {
            System.out.println("key: " + k + ", value:" + v);
        });

    }

 

    原文作者:leolu007
    原文地址: https://blog.csdn.net/Leolu007/article/details/87088068
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞