1、map(Student::getStuName).distinct() 集合中对象是否存在某个字段重复
// map(Student::getStuName).distinct() 集合中对象是否存在某个字段重复
List<Student> studentList = new ArrayList<>();
studentList.add(new Student(1,"张三"));
studentList.add(new Student(2,"李四"));
studentList.add(new Student(3,"张三"));
// map获取某字段,与distinct().count结合,判断是否存在重复
long count = studentList.stream().map(Student::getStuName).distinct().count();
if (count != studentList.size()) {
System.out.println("存在重复stuName的student");
}
输出结果
存在重复stuName的student
2、Collectors.toMap 获取集合中重复的对象
// Collectors.toMap 获取集合中重复的对象
studentList = new ArrayList<>();
Student zhangSan = new Student(1,"张三");
studentList.add<