List利用Java8特性stream根据集合中对象的某个属性去重

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
//根据id去重
        List unique1 = list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(User::getId))), ArrayList::new));
        System.out.println(unique1);
//根据id和mobile去重
        List unique2 = list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(o -> o.getId() + ";" + o.getMobile()))), ArrayList::new)
        );

 

你可能感兴趣的:(List利用Java8特性stream根据集合中对象的某个属性去重)