public class test{
List<entity> list=new ArrayList();
list.stream().collect(Collectors.toMap(x -> TimeUtil.getMonth(x.getMonth()), x -> x));
//以月份为key,截取list里面的月份
list.stream().
filter(x -> x.getType() != null).
collect(Collectors.groupingBy(x-> x.getType().substring(0,3), Collectors.summingInt(entity::getNum)));
//过滤后截取在计算
int sum = countPersonAgeDtos.stream().mapToInt(CountPersonAgeDto::getNum).sum();
//计算某个字段的总计
entity lastProgress = list.stream().max(Comparator.comparing(Entity::getTime)).get();
//获得list里面时间最大的对象
list.stream().filter(x -> x.getStudent() != null).collect(Collectors.toMap(entity::getStudent, x -> x));
//过滤空
long count = list.stream().distinct.count();
boolean isRepeat = count < list.size();
//去重后计数后判断长度,或判断是否有重复值且不需要重复结果
list.stream().collect(Collectors.toMap(x -> Optional.ofNullable(x.getTest()).orElse("null"), y -> y));
//防止空指针
list.stream().collect(Collectors.groupingBy(entity::getName))
//分组
Person result1 = collection.stream() // 转化为流
.filter(x -> "张三".equals(x.getName())) // 只过滤出"张三"
.findAny() // 如果找到了就返回
.orElse(null); // 如果找不到就返回null
list.stream().collect(Collectors.groupingBy(x -> {
//按照成员进行分组
//key为null 的返回null和对应的list
//x表示list 的每个对象
if (x != null) {
//这里是告诉这个函数通过什么分组
return x.getName();
} else {
//如果根据分组的这个key为空,根据null分组
return "未知";
}
}));
//根据要求分组,此处可以把null分为一组
list.stream().collect(Collectors.toMap(People::getName, p -> p, (k, v) -> k));
//Duplicate key 解决key重复问题,如果key重复,则覆盖掉一个
list.stream().
collect(Collectors.groupingBy(entity::getMonthNum,
Collectors.summarizingDouble(entity::getPractical)));
Map<Integer, Double> resultSet = new HashMap<>();
collect.forEach((k, v) -> resultSet.put(k, v.getSum()));
// collect.forEach((key,value) -> {
// double sum = value.getSum();这里的是获得sum, 等于上面这句话
//});
//分组后合计,在放进一个map
list.stream().sorted(Comparator.comparing(entity::getMonth).reversed()).collect(Collectors.toList());
//根据月份或者时间降序排序
list.forEach(item -> {
String dictValue = item.getDictValue();
if (egMap.containsKey(dictValue)) {
CountHighEdDto countHighEdDto = egMap.get(dictValue);
countHighEdDto.setHighEd(item.getDictLabel());
} else {
CountHighEdDto countHighEdDto = new CountHighEdDto(item.getDictLabel(), 0);
egMap.put(dictValue, countHighEdDto);
countHighEdDtos.add(countHighEdDto);
}
});
}
Stream流用法
最新推荐文章于 2024-07-06 16:47:20 发布