1. Stream.distinct()
distinct()返回由该流的不同元素组成的流。
distinct()是Stream接口的方法。
distinct()使用hashCode()和equals()方法来获取不同的元素。因此,我们的类必须实现hashCode()和equals()方法。
如果distinct()正在处理有序流,那么对于重复元素,将保留以遭遇顺序首先出现的元素,并且以这种方式选择不同元素是稳定的。
在无序流的情况下,不同元素的选择不一定是稳定的,是可以改变的。distinct()执行有状态的中间操作。
在有序流的并行流的情况下,保持distinct()的稳定性是需要很高的代价的,因为它需要大量的缓冲开销。如果我们不需要保持遭遇顺序的一致性, 那么我们应该可以使用通过BaseStream.unordered()方法实现的无序流。
在此示例中,我们有一个包含重复元素的字符串数据类型列表
Jdk8StreamDistinctDemo .java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @title Jdk8StreamDistinctDemo
* @Description Java 8 Stream.distinct() 列表去重示例
* @Date 2022/10/9 17:02
**/
public class Jdk8StreamDistinctDemo {
public static void main(String[] args) {
List<String> list = Arrays.asList("iphone11", "iphone12", "iphone13", "iphone14", "iphone14");
List<String> newList = list.stream().distinct().collect(Collectors.toList());
System.out.println("去重后newList:" + newList);
}
}
执行结果:
2. Stream.distinct() with List of Objects
在此示例中,我们有一个Iphone对象列表。 为了对列表进行去重,该类将重写hashCode()和equals()
Iphone .java
/**
* @title Iphone
* @Description TODO
* @Date 2022/10/9 17:11
**/
public class Iphone {
//名称
private String name;
//价格
private Integer price;
public Iphone(String name, Integer price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Iphone iphone = (Iphone) o;
return Objects.equals(name, iphone.name) && Objects.equals(price, iphone.price);
}
@Override
public int hashCode() {
return Objects.hash(name, price);
}
@Override
public String toString() {
return "" +
"name='" + name + '\'' +
", price=" + price +
'\n';
}
}
Jdk8StreamDistinctDemo .java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @title Jdk8StreamDistinctDemo
* @Description Java 8 Stream.distinct() 列表去重示例
* @Date 2022/10/9 17:02
**/
public class Jdk8StreamDistinctDemo {
public static void main(String[] args) {
List<Iphone> list = Arrays.asList(
new Iphone("iphone14", 5999),
new Iphone("iphone14 Plus", 6999),
new Iphone("iphone14 Pro", 7999),
new Iphone("iphone14 Pro", 7999),
new Iphone("iphone14 Pro Max", 8999)
);
List<Iphone> newList = list.stream().distinct().collect(Collectors.toList());
System.out.println("去重后newList:\n" + newList);
}
}
执行结果:
3. Distinct by Property
distinct()不提供按照属性对对象列表进行去重的直接实现。它是基于hashCode()和equals()工作的。如果我们想要按照对象的属性,对对象列表进行去重,我们可以通过其它方法来实现。如下代码段所示:
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;
}
distinctByKey()方法返回一个使用ConcurrentHashMap 来维护先前所见状态的 Predicate 实例,如下是一个完整的使用对象属性来进行去重的示例。
Jdk8StreamDistinctDemo .java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @title Jdk8StreamDistinctDemo
* @Description Java 8 Stream.distinct() 列表去重示例
* @Date 2022/10/9 17:02
**/
public class Jdk8StreamDistinctDemo {
public static void main(String[] args) {
List<Iphone> list = Arrays.asList(
new Iphone("iphone14", 5999),
new Iphone("iphone14 Plus", 6999),
new Iphone("iphone14 Pro", 7999),
new Iphone("iphone14 Pro", 7999),
new Iphone("iphone14 Pro Max", 8999)
);
List<Iphone> newList = list.stream().filter(distinctByKey(iphone -> iphone.getName())).collect(Collectors.toList());
System.out.println("去重后newList:\n" + newList);
}
private 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;
}
}
执行结果: