list<Bean> java8根据某几个属性去重

单个list 根据某几个属性去重

    /**
     * 过滤同一个List数据的策略
     *
     * @param function
     * @param 
     * @return
     */
    public static  Predicate distinctByKey(Function function) {
        Map seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;
    }

list<bean> 使用java8根据某几个属性去重复:使用链接

两个list 根据某几个属性去重

    /**
     * 过滤两个list数据的策略(bean对象可以不同)
     *
     * @param otherList     被比较的List
     * @param otherFunction 被比较的Function(目的是为了组装key)
     * @param thisFunction  调用此方法方的Function(目的是为了组装key)
     * @return
     */
    public static  Predicate distinctByOtherList(List otherList, Function otherFunction, Function thisFunction) {
        Set otherKeySet = otherList.stream().map(otherFunction).collect(Collectors.toSet());
        return e -> Boolean.TRUE.equals(otherKeySet.contains(thisFunction.apply(e)));
    }

两个 list<bean> 使用java8根据某几个属性去重复:使用链接

你可能感兴趣的:(java8,java)