can be iterated through the list, composing the name and sex attributes of each entity class into a string, and then using a HashSet to see if it is duplicated. If there is a duplicate, the entity class is added to a new list.
import java.util.*;
public class Person {
private int id;
private String name;
private int age;
private String sex;
public Person(int id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public static void main(String[] args) {
List persons = new ArrayList<>();
persons.add(new Person(1, "张三", 18, "男"));
persons.add(new Person(2, "张三", 18, "男"));
persons.add(new Person(3, "李四", 20, "男"));
persons.add(new Person(4, "李四", 18, "男"));
Set<String> uniquePerson = new HashSet<>();
List duplicatePerson = new ArrayList<>();
for (Person person : persons) {
String personInfo = person.getName() + person.getSex();
if (!uniquePerson.add(personInfo)) {
duplicatePerson.add(person);
}
}
System.out.println("重复的人员信息:");
for (Person person : duplicatePerson) {
System.out.println(person.getName() + " " + person.getSex());
}
}
}