集合去重
字符串去重
List<String> dataList = list.stream().distinct().collect(Collectors.toList());
对象属性去重
根据品牌名称去重
List<Brand> brandList = brandList .stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(Brand::getName))), ArrayList::new));
集合排序
排序的值不能为空,否则会报空指针异常
根据排序值升序排序
shopFloorFrontCategoryResponseList = shopFloorFrontCategoryResponseList.stream().filter(f->Objects.nonNull(f.getSort())).sorted(Comparator.comparing(ShopFloorFrontCategoryResponse::getSort)).collect(Collectors.toList());
根据排序值倒序排序
shopFloorFrontCategoryResponseList = shopFloorFrontCategoryResponseList.stream().filter(f->Objects.nonNull(f.getSort())).sorted(Comparator.comparing(ShopFloorFrontCategoryResponse::getSort).reversed()).collect(Collectors.toList());
获取集合中某个字段的最大值与最小值
最小值
Integer min = frontCategoryList.stream().mapToInt(FrontCategory::getSort).summaryStatistics().getMin();
最大值
Integer max = frontCategoryList.stream().mapToInt(FrontCategory::getSort).summaryStatistics().getMax();
获取集合中某个字段的和
Integer sum = frontCategoryList.stream().mapToInt(FrontCategory::getSort).sum();
分组
以categoryId分组
Map<Long, List<FrontCategoryBack>> map = frontCategoryBackList.stream().
filter(b -> Objects.nonNull(b.getCategoryId()))
.collect(Collectors.groupingBy(FrontCategoryBack::getCategoryId));