lambda知识点【干货】

集合去重

字符串去重

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));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容