通过filter方法可以过滤某些条件

list去重

 @GetMapping("get")
    public AjaxResult list(){
        List<CateType> cateTypeList=cateTypeService.list();
        //过滤
        //排除掉类型名为肉类的信息
        List<CateType> userCommonList = cateTypeList.stream().filter(a -> !a.getCareName().equals("肉类")).collect(Collectors.toList());
        return AjaxResult.success(userCommonList);
    }

这样就能过滤掉不要的信息啦!

   @GetMapping("get")
    public AjaxResult list(){
        List<CateType> cateTypeList=cateTypeService.list();
        //过滤
        //排除掉类型名相同的数据
        CateType cateType = new CateType();
        List<CateType> userCommonList = cateTypeList.stream().filter(distinctByKey(b -> b.getCareName())).collect(Collectors.toList());

        return AjaxResult.success(userCommonList);
    }


    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
原文地址:https://www.cnblogs.com/ckfeng/p/13876167.html