Java 集合根据对象属性去重
时间: 2023-09-26 14:11:39 浏览: 45
回答:可以使用Java 8中的stream和lambda表达式来实现,具体代码可参考以下示例:
```
List<Person> persons = new ArrayList<>();
// 添加Person对象
List<Person> uniquePersons = persons.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new
)
);
```
这段代码可以根据Person对象的name属性进行去重。
相关问题
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集合根据对象属性去重的方法,可以根据实际场景选择合适的方法。
java8 lamda集合 根据对象的属性去重
在Java 8中,可以使用Lambda表达式和Stream API实现根据对象属性去重的操作。
假设我们有一个Person类,其中包含属性name和age,我们需要根据name属性去重,可以按照以下步骤进行操作:
1. 创建一个List<Person>对象,包含多个Person对象,例如:
```
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 20));
personList.add(new Person("Bob", 22));
personList.add(new Person("Alice", 18));
```
2. 使用Stream API调用distinct()方法,并传入一个Lambda表达式,以根据name属性去重,例如:
```
List<Person> distinctList = personList.stream()
.filter(distinctByKey(Person::getName))
.collect(Collectors.toList());
```
3. 在distinctByKey()方法中,使用一个Map对象来存储已经出现过的name值,实现去重操作,例如:
```
public 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;
}
```
这样,我们就可以根据对象的属性去重,得到一个去重后的List对象。