目录
1.分组
2.去重
3.排序
4.过滤
5.求和
1.分组
//(按时间字段升序)
Map<String, List<RecycleIntroDataVo>> collect = dtos.stream()
.sorted(Comparator.comparing(RecycleIntroDataVo::getRecycleTime).reversed())
.collect(Collectors.groupingBy(RecycleIntroDataVo::getRecycleNameZHCN, LinkedHashMap::new, Collectors.toList()));
2.去重
list.stream().filter(distinctByKey(City::getCity)).collect(Collectors.toList());
3.排序
//获取对象中的id集合-升序
List<Integer> integerList = detailPageVoList.stream().sorted(Comparator.comparing(ArticleDetailPageVo::getId))
.map(ArticleDetailPageVo::getId).collect(Collectors.toList());
//获取对象中某个属性
List<String> list= cities.stream().map(City :: getCity).collect(Collectors.toList());
4.过滤
//排除掉工号为201901的用户
List<User> userCommonList = userList.stream().filter(a -> !a.getJobNumber().equals("201901")).collect(Collectors.toList());
5.求和
//最小值
Integer min = list.stream().mapToInt(User::getAge).min().getAsInt();
//最大值
Integer max = list.stream().mapToInt(User::getAge).max().getAsInt();
//平均值
Double average = list.stream().mapToInt(User::getAge).average().getAsDouble();
//和
Integer sum = list.stream().mapToInt(User::getAge).sum();
System.out.println("最小值:"+min+", 最大值"+max+", 平均值:"+average+", 和:"+sum);