stream流list去重
时间: 2023-09-26 14:11:36 浏览: 38
可以使用Java 8的Stream API来进行List的去重操作。下面是一个示例代码:
```java
List<String> list = Arrays.asList("a", "b", "c", "b", "d", "e", "a");
List<String> distinctList = list.stream().distinct().collect(Collectors.toList());
System.out.println(distinctList);
```
输出结果为:
```
[a, b, c, d, e]
```
在上面的代码中,我们先将一个包含重复元素的List转换成了Stream对象,然后使用`distinct()`方法进行去重操作,再使用`collect()`方法将结果收集到一个新的List中。
需要注意的是,这种去重方式保留了List中元素的顺序。如果要对List进行无序去重,可以使用`collect(Collectors.toSet())`方法。
相关问题
stream流list去重多字段
如果要对一个 `List` 中的对象进行去重,可以使用 Java 8 中的 `stream` 和 `distinct` 方法。假设我们有一个 `Person` 类,其中包含 `name` 和 `age` 两个字段,我们要对一个 `List<Person>` 进行去重,可以按照以下方法进行。
```java
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 20));
personList.add(new Person("Bob", 30));
personList.add(new Person("Alice", 20));
personList.add(new Person("Charlie", 40));
List<Person> distinctPersonList = personList.stream()
.distinct()
.collect(Collectors.toList());
```
这里我们使用 `distinct` 方法对 `stream` 中的元素进行去重操作,`distinct` 方法默认使用对象的 `equals` 方法进行判断是否相等,如果要自定义对象的相等规则,可以重写 `equals` 方法和 `hashCode` 方法。
我们可以在 `Person` 类中重写 `equals` 方法和 `hashCode` 方法,按照 `name` 和 `age` 两个字段进行判断是否相等。
```java
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
```
使用重写后的 `equals` 方法和 `hashCode` 方法,我们可以对 `Person` 对象进行按照 `name` 和 `age` 两个字段进行去重。
```java
List<Person> distinctPersonList = personList.stream()
.distinct()
.collect(Collectors.toList());
```
liststream流去重
使用Java8的stream流可以很方便地对List进行去重操作。stream流提供了多种方法来实现去重,下面是几种常见的方法:
1. 使用distinct()方法:可以通过list.stream().distinct()来对列表进行去重操作。这种方法适用于List中的元素为简单包装类型,或者自定义对象类型已经重写了equals和hashCode方法,根据业务情况来实现对象的相等判断。
2. 使用collect()方法和Collectors.toSet():可以使用stream流的collect()方法配合Collectors.toSet()来实现去重操作。这种方法会保留原始列表的顺序。示例代码如下:
```
List<String> list = Arrays.asList("A", "B", "A", "C", "B");
List<String> distinctList = list.stream().collect(Collectors.toSet()).stream().collect(Collectors.toList());
```
这样就可以得到去重后的列表distinctList,其中元素的顺序与原始列表保持一致。
3. 使用filter()方法和distinctByField()方法:如果需要根据对象的某个字段进行去重,可以使用stream流的filter()方法结合自定义的distinctByField()方法来实现。distinctByField()方法接收一个Function类型的参数,返回一个Predicate类型,用于根据指定字段进行去重。示例代码如下:
```
List<Person> list = Arrays.asList(new Person("Alice", 25), new Person("Bob", 30), new Person("Alice", 25));
List<Person> distinctList = list.stream().filter(distinctByField(Person::getName)).collect(Collectors.toList());
private static <T> Predicate<T> distinctByField(Function<? super T, ?> fieldExtractor) {
Set<Object> seen = ConcurrentHashMap.newKeySet();
return t -> seen.add(fieldExtractor.apply(t));
}
```
这样就可以根据Person对象的name字段进行去重,得到去重后的列表distinctList。、
综上所述,使用Java8的stream流可以很方便地对列表进行去重操作,可以根据需求选择不同的方法来实现。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>