java 流式处理案例

list对象根据ID个字段转为list<String>

 List<UserEntity> userEntitys=new ArrayList<>();
List<String> list =userEntitys.stream().map(UserEntity::getId).collect(Collectors.toList());

根据id字段来去重并且吧account这个值用逗号拼接起来,拿到是去重并且吧account凭借出来的一个对象
  List<UserEntity> list=new ArrayList<>();
        UserEntity entity1=new UserEntity();
        entity1.setId("1");
        entity1.setAccount("嘿嘿饿");
        list.add(entity1);
        UserEntity entity2=new UserEntity();
        entity2.setId("1");
        entity2.setAccount("嘿嘿");
        list.add(entity2);
        list= new ArrayList<>(list.stream()
                // 表示name为key,接着如果有重复的,那么从Pool对象o1与o2中筛选出一个,这里选择o1,
                // 并把name重复,需要将value与o1进行合并的o2, 赋值给o1,最后返回o1
                .collect(Collectors.toMap(UserEntity::getId, a -> a, (o1, o2) -> {
                    o1.setAccount(o1.getAccount() + "," + o2.getAccount());
                    return o1;
                })).values());
        System.err.println(list);

把list对象转化为map key为id value为对应的对象

 Map<String, UserEntity> collect = list.stream().collect(Collectors.toMap(UserEntity::getId, account -> account));

list对象转化为map key 和value都为制定字段

Map<String, String> collect = list.stream().collect(Collectors.toMap(UserEntity::getId, UserEntity::getAccount, (oldValue, newValue) -> newValue));

list对象 判断对象中的某一个值 然后取出 其他字段转化为list<String

// 假设你有一个名为objects的对象数组
        YourObject[] objects = {
                new YourObject("uuid1", 0),
                new YourObject("uuid2", 1),
                new YourObject("uuid3", 0),
                new YourObject("uuid4", 0)
        };

        // 找出state字段为0的对象,并将uuid字段转化为List<String>
        List<String> uuidList = Arrays.stream(objects)
                .filter(obj -> obj.getState() == 0)
                .map(YourObject::getUuid)
                .collect(Collectors.toList());

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值