前言
其他的在上面的转载地址里面。
补充内容
分组相关
https://blog.csdn.net/u010979642/article/details/98639064
数字求均值
List<Customer> list = new ArrayList<>();
Customer c1 = new Customer();
c1.setId(1L);
list.add(c1);
Customer c2 = new Customer();
c2.setId(3L);
list.add(c2);
LongSummaryStatistics lss = list.stream().collect(Collectors.summarizingLong(Customer::getId));
System.out.println(lss);
数字求和
long sum = list.stream().reduce(Integer::sum).orElse(0);
对象求和
转载自:https://blog.csdn.net/hsz2568952354/article/details/89886743
// 直接求和
integerListEntry.getValue().stream().map(ConsultantDailyReport::getIndicatorValue).reduce(Integer::sum).orElse(0);
// 过滤后求和
Integer integer = integerListEntry.getValue().stream().filter(a -> a.getIndicatorValue().equals(1)).map(ConsultantDailyReport::getIndicatorValue).reduce(Integer::sum).orElse(0);
集合转map 使用扩展
修改key
list.stream().collect(Collectors.toMap(Student::getClassName, Student::getStudentName,
(value1, value2 )->{
return value2;
}));
改变key的类型
statusMap = statusList.stream().collect(Collectors.toMap(a->Integer.valueOf(a.getValue()), a -> a.getName(), (k1, k2) -> k1));
list转换类型
customerOutpatientReportTypes.stream().map(
outpatientReportType -> {
CustomerOutpatientReportTypeQueryVo queryVo = new CustomerOutpatientReportTypeQueryVo();
queryVo.setId(outpatientReportType.getId());
queryVo.setName(outpatientReportType.getName());
queryVo.setType(outpatientReportType.getType());
return queryVo;
}
).collect(Collectors.toList());
list转set
Set<Integer> roleIds = baseRoles.stream().map(BaseRole::getId).collect(Collectors.toSet());
list根据参数去重
java8去重:根据年级和专业,当年级和专业都相同的情况下看做是重复数据
List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));
java8去重:根据对象内的地址去重,当有对象内地址一样的时候看做是重复数据
List<ApiUrlInfoDto> distinctClass = apiUrlInfoDtos.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getUrl()))), ArrayList::new));
枚举转map
Map<String, String> collect = Arrays.stream(OperLogTypeEnum.values()).collect(Collectors.toMap(OperLogTypeEnum::getTypeName, OperLogTypeEnum::getTypeValue));