stream流常用方法

把list集合中数据一样的记录去重
List<entity> collect = list.stream().distinct().collect(Collectors.toList());

list集合根据某个字段进行分组
Map<Long,List<entity>> workOrderNeedMap = list.stream().collect(Collectors.groupingBy(entity::getNeedId));

list集合根据某个字段进行去重
ArrayList<entity> collect = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(entity::getId))), ArrayList::new));

list集合根据多个字段进行去重
List<CaveCollectionBean> newCaveCollection= list.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));


获得集合中某个字段的集合
List<String> userNameList = sysUserList.stream().map(SysUser::getUsername).collect(Collectors.toList());

根据entity.getId()获得list集合中id相等的数据
List<entity> collect = list.stream().filter(p -> p.getId().equals(entity.getId())).collect(Collectors.toList())
//根据某字段进行降序排序 (reversed 降序 不用则默认升序)
List<AppletWorkingHoursVo> workingHoursVos = appletWorkingHoursVos.stream().sorted(Comparator.comparing(AppletWorkingHoursVo::getStartTime).reversed()).collect(Collectors.toList());

//根据某字段进行升序排序
List<AppletWorkingHoursVo> workingHoursVos = appletWorkingHoursVos.stream().sorted(Comparator.comparing(AppletWorkingHoursVo::getStartTime)).collect(Collectors.toList());
Stream流对某个数字类型字段求和的方法


List<User> list = userService.list(wrapper);
// int类型
int age = list.stream().mapToInt(User::getAge).sum();
// long类型
long age = list.stream().mapToLong(User::getAge).sum();
// double类型
double age = list.stream().mapToDouble(User::getAge).sum();
// BigDecimal 类型
BigDecimal age = list.stream().map(User::getAge).reduce(BigDecimal.ZERO, BigDecimal::add);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值