Java 集合根据对象属性去重
时间: 2023-09-26 09:11:39 浏览: 101
回答:可以使用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集合根据对象属性去重的方法,可以根据实际场景选择合适的方法。
java集合中根据对象属性去重并记录重复的数据的位置
可以使用Java中的Map和List来实现根据对象属性去重并记录重复数据的位置。
首先,定义一个对象类,假设这个类叫做Person,有一个属性叫做name:
```
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
接着,定义一个Map,用来存储去重后的数据和对应的位置:
```
Map<Person, List<Integer>> map = new HashMap<>();
```
然后,遍历原始数据的List,对于每个Person对象,将其作为Map的key进行判断:
```
List<Person> originalList = ... // 原始数据
for (int i = 0; i < originalList.size(); i++) {
Person person = originalList.get(i);
if (map.containsKey(person)) {
// 如果已经存在该对象,则将当前位置添加到对应List中
map.get(person).add(i);
} else {
// 如果不存在该对象,则创建一个新的List,并将当前位置添加到List中
List<Integer> list = new ArrayList<>();
list.add(i);
map.put(person, list);
}
}
```
最终,可以遍历Map,得到去重后的数据和对应的位置:
```
for (Map.Entry<Person, List<Integer>> entry : map.entrySet()) {
Person person = entry.getKey();
List<Integer> positions = entry.getValue();
System.out.println(person.getName() + " 重复位置:" + positions);
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)