List Map 根据指定的字段进行去重处理

 

    private  <T> Predicate<T> distinctByKeys(Function<? super T, Object> ...keyExtractors) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();

        return t -> {
            final List<?> keys = Arrays.stream(keyExtractors)
                    .map(ke -> ke.apply(t))
                    .collect(Collectors.toList());

            return seen.putIfAbsent(keys, Boolean.TRUE) == null;
        };
    }
List<Map> resultMap = listMap.stream()
                .filter(distinctByKeys(p->p.get("invoiceCode"),p->p.get("invoiceNum")))
                .collect(Collectors.toList());

 

posted @ 2022-05-25 15:15  yshy  阅读(845)  评论(0编辑  收藏  举报