java8 快速实现List转map 、分组、过滤等操作

前言

其他的在上面的转载地址里面。

补充内容

分组相关

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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值