Java 8 Stream 流根据属性去重

目录

distinct 去重

使用 Set 特性去重

通过 filter() 方法

使用分组


distinct 去重

distinct()是Java 8 中 Stream 提供的方法,返回的是由该流中不同元素组成的流。distinct()使用 hashCode() 和 eqauls() 方法来获取不同的元素。因此,需要去重的类必须实现 hashCode() 和 equals() 方法。换句话讲,我们可以通过重写定制的 hashCode() 和 equals() 方法来达到某些特殊需求的去重。
缺点:如果List集合元素为对象,去重不会奏效(除非重写equals与hashcode方法

List<Person> collect1 = list.stream().distinct().collect(Collectors.toList());

使用 Set 特性去重

缺点:该方式不能保持原列表顺序而是使用了TreeSet按照字典顺序排序后的列表,如果需求不需要按原顺序则可直接使用。

ArrayList<Person> collect = list.stream().collect(Collectors.collectingAndThen(
    Collectors.toCollection(
        () -> new TreeSet<>(Comparator.comparing(Person::getFirstName))), ArrayList::new));

`Collectors.collectingAndThen()`:可接受两个参数,第一个参数用于 reduce操作,而第二参数用于 map操作。也就是,先把流中的所有元素传递给第一个参数,然后把生成的集合传递给第二个参数来处理。

`Collectors.toCollection()`:把集合中的元素转换成参数指定的集合类型进行保存。

通过 filter() 方法

我们首先创建一个方法作为 Stream.filter() 的参数,其返回类型为 Predicate,原理就是判断一个元素能否加入到 Set/Map 中去,代码如下:

// https://www.concretepage.com/java/jdk-8/java-8-distinct-example
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor){
    ConcurrentHashMap<Object, Boolean> concurrentHashMap = new ConcurrentHashMap<>();
    return  t -> concurrentHashMap.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

// https://stackoverflow.com/questions/23699371/java-8-distinct-by-property
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor){
    Set<Object> seen = ConcurrentHashMap.newKeySet();
    return t -> seen.add(keyExtractor.apply(t));
}

List<Person> collect = list.stream().filter(distinctByKey(Person::getFirstName)).collect(Collectors.toList());

使用分组

List<Person> collect = list.stream().collect(Collectors.groupingBy(Person::getFirstName))
                .values().stream()
                .flatMap(group -> group.stream().limit(1))
                .collect(Collectors.toList());

猜你喜欢

转载自blog.csdn.net/weixin_46058921/article/details/126940132