list集合用stream流根据对象属性去重
时间: 2023-09-26 20:11:39 浏览: 53
可以使用 Java 8 的 Stream API 的 distinct() 方法来根据对象属性去重。假设有一个 List<Student>,其中 Student 类有一个 name 属性,可以按照如下方式去重:
```
List<Student> distinctStudents = students.stream()
.filter(distinctByKey(Student::getName))
.collect(Collectors.toList());
```
其中,distinctByKey() 方法的实现如下:
```
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
```
该方法返回一个 Predicate,该 Predicate 会根据 keyExtractor 提取的属性值去重。这里使用了 ConcurrentHashMap 来保证线程安全。
相关问题
stream流根据某个属性去重
在Java中,可以使用Stream流根据某个属性去重。下面是一个示例代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Alice", 25));
personList.add(new Person("Charlie", 35));
List<Person> distinctPersons = personList.stream()
.collect(Collectors.toMap(Person::getName, person -> person, (p1, p2) -> p1))
.values()
.stream()
.collect(Collectors.toList());
distinctPersons.forEach(person -> System.out.println(person.getName() + " - " + person.getAge()));
}
}
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;
}
}
```
上述代码中,我们创建了一个Person类,包含name和age属性。我们使用Stream流对personList进行去重操作,根据name属性进行去重。具体步骤如下:
1. 使用`collect(Collectors.toMap(...))`将List转换为Map,其中key为name属性,value为对应的Person对象。
2. 使用`.values()`获取Map中的所有value,即去重后的Person对象集合。
3. 使用`collect(Collectors.toList())`将去重后的Person对象集合转换为List。
4. 最后,使用`forEach`遍历去重后的Person对象集合,并输出name和age属性。
运行上述代码,输出结果为:
```
Alice - 25
Bob - 30
Charlie - 35
```
list集合根据对象属性去重的多种方法实现
有多种方法可以实现List集合根据对象属性去重,以下是其中几种常用的方法:
1. 重写对象的equals和hashCode方法
重写对象的equals和hashCode方法,根据属性判断两个对象是否相等,然后将对象放入Set集合中去重。示例代码如下:
```java
public class Person {
private String name;
private int age;
// 省略构造方法和getter/setter方法
@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);
}
}
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", 20));
personList.add(new Person("李四", 22));
personList.add(new Person("张三", 20));
Set<Person> personSet = new HashSet<>(personList);
System.out.println(personSet);
```
输出结果为:`[Person{name='张三', age=20}, Person{name='李四', age=22}]`
2. 使用Java 8的stream和distinct方法
使用Java 8的stream和distinct方法,根据属性去重。示例代码如下:
```java
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", 20));
personList.add(new Person("李四", 22));
personList.add(new Person("张三", 20));
List<Person> distinctPersonList = personList.stream().distinct().collect(Collectors.toList());
System.out.println(distinctPersonList);
```
输出结果为:`[Person{name='张三', age=20}, Person{name='李四', age=22}]`
3. 使用Java 8的stream和toMap方法
使用Java 8的stream和toMap方法,根据属性去重。示例代码如下:
```java
List<Person> personList = new ArrayList<>();
personList.add(new Person("张三", 20));
personList.add(new Person("李四", 22));
personList.add(new Person("张三", 20));
List<Person> distinctPersonList = personList.stream().collect(Collectors.toMap(
person -> person.getName() + "-" + person.getAge(),
person -> person,
(oldValue, newValue) -> oldValue)).values().stream().collect(Collectors.toList());
System.out.println(distinctPersonList);
```
输出结果为:`[Person{name='张三', age=20}, Person{name='李四', age=22}]`
以上是几种常用的List集合根据对象属性去重的方法,可以根据实际场景选择合适的方法。