stream list 指定字段去重

直接上代码

import java.util.function.Function;
import java.util.function.Predicate;


static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
     
    Map<Object,Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
} 

去除部门相同的

deptNotPublish.stream().filter(distinctByKey(b -> b.getRespDeptId())).collect(Collectors.toList());

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