一、基础概念
二、特性
1.分组
groupby方法有几个重载方法,上面使用的方法有三个参数
第一个参数表示分组按照什么进行分类
第二个参数表示分组结果最后用什么容器保存并返回,可以指定为LinkedHashMap,HashMap等
第三个参数表示分类后,对应的分类的结果如何收集
踩坑分享
1.顺序不一致
HashMap是无序的,HashMap在put的时候是根据key的hashcode进行hash然后放入对应的地方。所以在按照一定顺序put进HashMap中,然后遍历出HashMap的顺序跟put的顺序不同
解决办法使用LinkedHashMap容器存储
LinkedHashMap<String, List<Brand>> brandMap = brandList.stream().collect(Collectors.groupingBy(Brand::getFirstLetter, LinkedHashMap::new, Collectors.toList()));
2.分组求和
可以采用先分组后求和思路
Map<String, List<DeviceHisTelemetry>> dailyGroup = ListUtil.nullToEmpty(list)
.stream().collect(Collectors.groupingBy(this::groupOfDaily, Collectors.toList()));
HistogramVo histogramVo = HistogramVo.empty();
List<BigDecimal> actualList = new ArrayList<>();
dailyGroup.forEach((key, value) -> {
histogramVo.getX().add(key);
//投喂时长之和
long relayOnDurationSum = ListUtil.nullToEmpty(value).stream()
.filter(e -> Objects.equals(TimeSeriesConstant.RELAY_ON_DURATION, e.getTimeSeriesKey()))
.map(DeviceHisTelemetry::getLongV)
.filter(Objects::nonNull)
.reduce(0L, Long::sum)
.longValue();
actualList.add(calculateFeedNum(relayOnDurationSum));
});
private String groupOfDaily(DeviceHisTelemetry deviceHisTelemetry) {
Long ts = deviceHisTelemetry.getTs();
return DateUtil.formatDate(new Date(ts));
}
2.数学计算
1.求和用reduce的方式
如下面代码:
//锁单数量
long lockQuantity = ListUtil.nullToEmpty(statDateCooperates).stream()
.filter(e -> Objects.equals(CustomStatusEnum.ALREADY_DETERMINE.getName(), e.getCustomStatus()))
.map(ProduceSaleCooperate::getScrollQuantity)
.filter(Objects::nonNull)
.reduce(0, Integer::sum)
.longValue();
这样能够避免元素为空造成的异常。
而下面这种方式会因为元素为空而造成异常
/**
* 无法处理value为null情况!!慎用!!
* @Deprecated
* @param list
* @param mapper
* @return
* @param <T>
*/
public static <T> LongSummaryStatistics summaryStatistic(List<T> list, ToLongFunction<? super T> mapper) {
return nullToEmpty(list).stream().collect(Collectors.summarizingLong(mapper));
}
3.map
map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
1.重复键问题
对于重复的key,会报冲突异常。
解决办法:
1.过滤重复的key
public static <T, K, R> Map<K, R> toMap(Collection<T> collection, Function<T, K> keyMapper,
Function<T, R> valueMapper) {
if (CollectionUtils.isEmpty(collection)) {
return Maps.newHashMap();
}
return collection.stream()
.filter(Predicates.distinctByKey(keyMapper))
.collect(Collectors.toMap(keyMapper, valueMapper));
}
2.自定义取新值还是老值
4.排序
1.处理null情况
1.正确用法
list.sort(Comparator.nullsLast(
Comparator.comparing(WorkshopInfoVo::getWorkshopSort,Comparator.nullsLast(Comparable::compareTo))));
错误用法,下面这种情况如果字段为空还是会出现NPE异常
list.sort(Comparator.nullsLast(Comparator.comparing(WorkshopInfoVo::getWorkshopSort)));
三、Optional
1.NPE优雅处理
使用举例
@Test
public void ofNullableMapTest() {
Apple apple = Apple.builder().build();
Integer id = Optional.ofNullable(apple)
.map(Apple::getPear)
.map(Pear::getId)
.orElse(null);
System.out.println(id);
}
参考资料
3.Lambda 表达式有何用处 https://blog.csdn.net/GoGleTech/article/details/79454151