1、测试实体类
import lombok.Data;
@Data
public class Person {
private String name;
private String code;
public Person(String name,String code){
this.name=name;
this.code=code;
}
}
2、测试代码
@Test
void contextLoads() throws InterruptedException {
Set<Person> hashSet=new HashSet<>();
List<Person> list=new ArrayList<>();
list.add(new Person("t1","2017"));
list.add(new Person("t2","2019"));
list.add(new Person("t3","2018"));
list.add(new Person("t1","2017"));
list.add(new Person("t2","2019"));
list.add(new Person("t3","2018"));
hashSet.addAll(list);
System.out.println("列表结果");
System.out.println(list);
System.out.println("去重后结果");
System.out.println(hashSet);
}
3、查看结果
列表结果
[Person(name=t1, code=2017), Person(name=t2, code=2019), Person(name=t3, code=2018), Person(name=t1, code=2017), Person(name=t2, code=2019), Person(name=t3, code=2018)]
去重后结果
[Person(name=t2, code=2019), Person(name=t1, code=2017), Person(name=t3, code=2018)]