没长正的技术专栏 勤动手、多思考

Lambda表达式

2017-03-01
mzz

阅读:

2022-04-11

目的

了解便于快速运用到项目中。

一、分组

1. Map<Object,Object>分组

Map<Long, MtPointAlarmStaticsDay> dayMap = dayList.stream().
collect(Collectors.toMap(MtPointAlarmStaticsDay::getPointConfId, 
Function.identity(), (k1, k2) -> k2));
//k1,k2 表示value 值

2. Map<Object,List> 分组

// 常规分组
Map<City, Set<String>> namesByCity=people.stream().collect(
groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));
list.stream().collect(Collectors.groupingBy(e->e.getProperty()));

3. Map<Object,List> 分组并多列求和

Map<Long, MtPointAlarmStaticsHour> hourMap = hourList.stream().filter(Objects::nonNull)
                .filter(e -> e.getStatisticDate().compareTo(minNow) == 0)
                .collect(Collectors.toMap(k -> k.getPointConfId(),
                        v -> new MtPointAlarmStaticsHour().setPointConfId(v.getPointConfId())
                                .setStatisticDate(v.getStatisticDate())
                                .setAlarmNum(v.getAlarmNum())
                                .setDeviceNum(v.getDeviceNum())
                                .setGatewayNum(v.getGatewayNum()), (x, y) -> {
                            x.setAlarmNum(x.getAlarmNum() + y.getAlarmNum());
                            x.setGatewayNum(x.getGatewayNum() + y.getGatewayNum());
                            x.setDeviceNum(x.getDeviceNum() + y.getDeviceNum());
                            return x;
                        }));
3.1 单列分组求和
Map<Long,Integer>
   browseMap = browseList.stream().
   collect(Collectors.groupingBy(SkAccidentBrowseRecord::getAccidentId, Collectors.summingInt(SkAccidentBrowseRecord::getBrowseNum)));

3.2 单列分组计数

Map<String, Long> monitorTypeMap = pointConfList.stream()
                .collect(Collectors.groupingBy(p -> p.getMonitorType(), Collectors.counting()));
3.3 多字段分组
//多个字段,分组求和(先按datas分组,再按Carrierid分组,求和)
Map<String, Map<String, LongSummaryStatistics>> enusersCollect2 =
        li.stream().collect(Collectors.groupingBy(DataStatisticsResultMiddle::getDatas,
        Collectors.groupingBy(DataStatisticsResultMiddle::getCarrierid,
            Collectors.summarizingLong(DataStatisticsResultMiddle::getEnusers))));

3.4 Bigdecimal 字段分组求和
// 聚合各个指标年标准值
Map<Long, BigDecimal> monitorIndexMap = relList.stream().collect(Collectors.groupingBy(HjDischargeMonitorItemRet::getMonitorItemId,
        Collectors.collectingAndThen(Collectors.toList(), m -> {
            return m.stream().map(v -> v.getYearStandardQuantity())
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        })));

二、排序

1.单字段排序

// 1. 集合方式
userList.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));

// 2. Comparator
 userList.sort(Comparator.comparing(UserVO::getAge));

// 3. Stream方式 排完序后倒序
List<UserVO>  ret = userList.stream()
    .sorted(Comparator.comparing(UserVO::getAge).reversed())
    .collect(Collectors.toList());

// 4. Stream方式 直接计算倒序
List<UserVO> ret2 = userList.stream()
    .sorted(Comparator.comparing(UserVO::getAge,Comparator.reverseOrder()))
    .collect(Collectors.toList());

2.多字段排序

// 1.集合方式
Collections.sort(userList, Comparator.comparing(UserVO::getName)
                .thenComparingInt(UserVO::getAge));
// 2.Stream 方式
List<UserVO> ret = userList.stream()
               .sorted(Comparator.comparing(UserVO::getName).thenComparingInt(UserVO::getAge))
                .collect(Collectors.toList());

3. 空数据排序

//1. 集合方式
Collections.sort(userList, Comparator.nullsFirst(Comparator.comparing(UserVO::getAge)).reversed());

// 2.Stream 方式
List<UserVO> ret = userList.stream()               .sorted(Comparator.nullsFirst(Comparator.comparing(UserVO::getAge,Comparator.nullsLast(String::compareTo))))
                .collect(Collectors.toList());

参考

java lambda表达式 集合(多)字段排序


欢迎拍砖,多多交流,转载请注明出处:[没长正的技术专栏](http://blog.meizhangzheng.com) 如涉及侵权问题,请发送邮件到xsj34567@163.com,如情况属实本人将会尽快删除。


上一篇 算法扩展

下一篇 Java常见问题

Comments

Content