使用Java 8 Stream.distinct() 列表对List数据去重

一、去除List中重复的String

List newList = list.stream().distinct().collect(Collectors.toList());

根据cakeName去重

            List<CakeInfo> newList = list.stream().collect( Collectors.collectingAndThen(Collectors.toCollection(() ->
                        new TreeSet<>(Comparator.comparing(CakeInfo::getCakeName))), ArrayList::new));

根据cakeName和cakeEncode去重

List<CakeInfo> newList= list.stream().collect(
           Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCakeName() + ";" + o.getCakeEncode()))), ArrayList::new)
);

你可能感兴趣的:(jdk)