List使用Lambda转换元素

class User {
    Integer id;
    String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class UserQuery {
    Integer id;
    String name;

    public UserQuery() {
    }

    public UserQuery(Integer id, String name) {
        this.id = id;
        this.name = name;
    }


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
 //转换list对象
        userList.clear();
        userList.add(new User(3, "Tom3"));
        userList.add(new User(2, "Tom2"));
        userList.add(new User(1, "Tom1"));

        List<UserQuery> userQuerylist = new ArrayList<>();

        userList.stream().forEach(user -> {
            UserQuery userQuery = new UserQuery();
            userQuery.setId(user.getId());
            userQuery.setName(user.getName());
            userQuerylist.add(userQuery);
        });

    原文作者:二十六画生的博客
    原文地址: https://blog.csdn.net/u010002184/article/details/79714190
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞