目的
了解便于快速运用到项目中。
一、分组
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());