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 首次发布