1.定义一个对象Student;属性有姓名,年龄,性别 先要求获取性别为男性的学生并且姓名相同的进行去重。 List<Student> list=new ArrayList<>(); mayDay23InviteHelpRecordsList.stream(). //过滤 filter(student ->student.getSex.equals("男")). //去重 collect(Collectors.collectingAndThen(Collectors.toCollection(() ->new TreenSet<>(Comparator.comparing(student::getName))),ArrayList::new)).collect(Collectors.toList()); 2.循环list为另一个对象赋值 list.stream(). //倒序排序 .sorted(Comparator.comparing(MayDay23InviteHelpRecord::getHelpDateTime).reversed()) .map(student ->{ A a = new A(); a.setName(student.getName()).setSex(student.getSex()); return a; }).collect(Collectors.toList());
stream流的使用
于 2023-04-27 10:47:12 首次发布
文章讲述了如何使用Java的StreamAPI来处理List对象,首先定义了一个Student对象,包含姓名、年龄和性别属性。接着,通过Stream的filter方法过滤出性别为男性的学生,并利用Collectors的去重功能,按照姓名进行去重。然后,将结果转换回List。此外,还展示了如何对列表进行倒序排序,并将排序后的学生信息赋值给新的Aa对象。
摘要由CSDN通过智能技术生成