Java List找出重复元素:
import com.alibaba.fastjson.JSON;
import lombok.Data;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Data
public class Person {
private String name;
private int age;
private String sex;
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
Person person1 = new Person();
person1.setName("张三");
person1.setAge(18);
personList.add(person1);
Person person2 = new Person();
person2.setAge(30);
person2.setName("李四");
personList.add(person2);
Person person3 = new Person();
person3.setName("王五");
person3.setAge(25);
personList.add(person3);
Set<String> uniqueSet = new HashSet<>();
List<Person> repeatList = personList.stream().filter(person -> !uniqueSet.add(person.getName())).collect(Collectors.toList());
System.out.println(JSON.toJSONString(personList));
System.out.println(JSON.toJSONString(repeatList));
}
}