Lamda表达式,map和集合操作

1、Lamda表达式,map快速获取对象的某一个属性生成集合

List<String> elementSlugs = elementInstanceBOList.stream().map( elementInstance->elementInstance.getSlug()).collect( Collectors.toList());

2、实现将一个List的属性赋值给另一个List,只用一层循环

            List<AlarmRecordDO> alarmRecordDOS = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(page.getContent())){
                alarmRecordDOS=mapper.mapList(page.getContent(),AlarmRecordDO.class);
                List<AlarmStrategyPO> alarmStrategyPOS=listAllAlarmStrategy();
                //将 alarmStrategyPO的oid和name构建一个map
                final Map<Long, String> map=alarmStrategyPOS.stream().collect(Collectors.toMap(AlarmStrategyPO::getOid,
                        AlarmStrategyPO::getName));
                alarmRecordDOS.stream().forEach(alarmRecordDO -> {
                    alarmRecordDO.setAlarmStrategyName(map.get(alarmRecordDO.getAlarmStrategyId()));
                });
                metadata.getPaginationParam().setTotalCount((int) page.getTotalElements());

            }
原文地址:https://www.cnblogs.com/boshen-hzb/p/7145378.html