摘抄大佬:List根据对象属性去重
List stringList = Arrays.asList("1", "2","3","1","1","1","1");
Set set = new LinkedHashSet<>();
set.addAll(stringList);
stringList.clear();
stringList.addAll(set);
System.out.println(stringList);
报错异常:解惑答疑
List stringList = Arrays.asList("1", "2","3","1","1","1","1");
Set set = new LinkedHashSet<>();
set.addAll(stringList);
// stringList.clear();
// stringList.addAll(set);
System.out.println("set================"+set);
System.out.println("list================"+stringList);
}
List unique = list.stream().distinct().collect(Collectors.toList());
比如现在有一个 Person类:
public class Person {
private Long id;
private String name;
public Person(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
重写Person对象的equals()
方法和hashCode()
方法:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (!id.equals(person.id)) return false;
return name.equals(person.name);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + name.hashCode();
return result;
}
下面对象去重的代码:
Person p1 = new Person(1l, "jack");
Person p2 = new Person(3l, "jack chou");
Person p3 = new Person(2l, "tom");
Person p4 = new Person(4l, "hanson");
Person p5 = new Person(5l, "胶布虫");
List persons = Arrays.asList(p1, p2, p3, p4, p5, p5, p1, p2, p2);
List personList = new ArrayList<>();
// 去重
persons.stream().forEach(
p -> {
if (!personList.contains(p)) {
personList.add(p);
}
}
);
System.out.println(personList);
这个是重点部分了
实体类
package com.yzg.array;
public class Student {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
去重实现
package com.yzg.array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
public class StuArray {
public static void main(String[] args) {
Student s=new Student();
s.setId(1);
s.setName("小明");
Student s1=new Student();
s1.setId(2);
s1.setName("小红");
Student s2=new Student();
s2.setId(2);
s2.setName("小甘");
List stu= Arrays.asList(s,s1,s2);
for (Student t: stu
) {
System.out.println(t.toString());
}
//去重
List list=stu.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(Student::getId))), ArrayList::new));
System.out.println("移除后的数组=========================");
for (Student t: list
) {
System.out.println(t.toString());
}
}
}
换成对应的实体类,和需要去重的属性即可
import java.util.TreeSet; import static java.util.Comparator.comparingLong; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toCollection;//去重 Listlist=stu.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(comparingLong(Student::getId))), ArrayList::new));