jdk1.8 lamda表达式操作stream流List全纪录

分组

对list分组,并返回Map结构

Map<String, List<Equipment>> collect = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getState));

两次分组,返回双重的map结构:

Map<EquipmentIndexType, Map<EquipmentState, List<Equipment>>> collect = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getIndexType, Collectors.groupingBy(v -> v.getState())));

分组后根据某个属性再转换为map

Map<EquipmentIndexType, Map<String, VO>> indexTypeMapMap = statisticMapper.getSafeMonthAlarmAnalysis(reqVO).stream().collect(Collectors.groupingBy(VO::getIndexType,Collectors.toMap(VO::getMonth, v -> v)));

对list分组,并返回各个分组的统计个数

Map<String, Long> map = equipmentList.stream().collect(Collectors.groupingBy(Equipment::getType, Collectors.counting()));

多个字段分组

Map<String, List<TbSapBankFlowDictionary>> sapDicMap = sapDictionaryList.stream().collect(Collectors.groupingBy(v -> v.getPayCode() + "_" + v.getReceiveCode()));

过滤:

条件过滤

taskList.stream().filter(v -> null != v.getEndTime()).collect(Collectors.toList());

条件过滤并统计复合条件的个数

taskList.stream().filter(v -> null != v.getEndTime()).collect(Collectors.toList()).size();

去重

distinct()

String voucherNo = res.getETFIDOC().getItem().stream().map(ZFZETFIDOC::getBELNR).distinct().collect(Collectors.joining(","));

实体转换

一种list实体类转为另一种list实体类

List<SysOrganizationUser> organizationUserList = v.stream().map(orgId -> {
                SysOrganizationUser organizationUser = new SysOrganizationUser();
                organizationUser.setUserId(userInfo.getId());
                organizationUser.setOrganizationId(orgId);
                organizationUser.setCreateTime(new Date());
                organizationUser.setCreateUser(UserUtil.getUserId());
                return organizationUser;
            }).collect(Collectors.toList());

List<T> 转 Map<String, List<T>>

Map<Integer, List<Student>> map = stu.stream().collect(Collectors.groupingBy(e -> e.getId()));

Map相关操作

list结构根据某个字段属性转Map,某个属性作为Map的key

// v -> v.getId()指作为map的key的字段,v -> v是指作为value的字段,(v1, v2) -> v1)是指发生key冲突时选择哪一个value作为map的value
Map<Long, DataTemplateItem> item = items.stream().collect(Collectors.toMap(v -> v.getId(), v -> v, (v1, v2) -> v1));

字符串拼接

// Collectors.joining
String set = vo.getList().stream().map(v -> v.getItemId().toString()).collect(Collectors.joining(","));

根据某一对象字段去重

//1、
List<OralDetectionOss> ossList = detectionOssList
                .stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(OralDetectionOss::getType)
                                        )
                                ), ArrayList::new
                        )
                );

//2 重写实体equals、hashcode方法 
使用infoList.stream().distinct().collect(Collectors.toList());

字段求和

BigDecimal sumAmount = Optional.ofNullable(moneyDetailDtos).orElse(Collections.emptyList()).stream().map(MoneyDetailDto::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

持续更新中。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值