Java8 list根据对象某个属性去重

2021-07-13 16:06

阅读:484

标签:ica   super   actor   cti   ati   object   lis   属性   util   

1. 添加方法:

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

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

2. 使用:

facultyRetList = facultyList.stream().filter(distinctByKey(Faculty::getId)).collect(Collectors.toList());

 

Java8 list根据对象某个属性去重

标签:ica   super   actor   cti   ati   object   lis   属性   util   

原文地址:https://www.cnblogs.com/miaoying/p/9542002.html


评论


亲,登录后才可以留言!