• Java stream().map()将对象转换为其他对象


     

     

    1: 將對象List轉為List<String>

    复制代码
    public class user{
        private String name;
        private String password;
        private String address;
        private String age;
     }
    复制代码
    List<String> name= user.stream().map(x -> x.getName()).collect(Collectors.toList());

    2: 將List<String> 轉為對象list
    复制代码
     List<User> result = staff.stream().map(name-> {
                User user= new User();
                user.setName(name);
                user.setPassword(null);
                user.setAddress(null);
                user.setAge(null);
                return user;
            }).collect(Collectors.toList());
    复制代码

    3:將一個對象轉為另一個對象

    复制代码
    public class UserInfo {
        private String name;
        private String pwd;
    }

    // 需指定對應字段
    List<UserInfo> collect = user.stream()

    .map(l -> new UserInfo(l.getName(), l.getPassword())).collect(Collectors.toList());

     

  • 相关阅读:
    BZOJ 1631 Cow Party
    BZOJ 1927 星际竞速
    BZOJ 4059 Non-boring sequences
    BZOJ 1562 变换序列
    BZOJ 4417 超级跳马
    484586
    背板问题之满包问题
    对01背包路径的记录
    带权值的图 BFS
    漫步校园 杭电1428
  • 原文地址:https://www.cnblogs.com/cfas/p/16066141.html
Copyright © 2020-2023  润新知