@Test
public void test(){
Student student = new Student();
List<Student> list = student.StudentList();
List<String> list1 = list.stream()
.filter(Objects::nonNull) //过滤不为空
.filter(item -> item.getAge() == 20 || item.getAge() == 21) //过滤条件
.sorted(Comparator.comparing(Student::getAge).reversed()) //倒叙排序,正序把reversed去掉即可
.map(Student::getName)//只要名字,其他的不要
.distinct() //去重复
.collect(Collectors.toList()); //装进这个盒子里
list1.forEach(System.out::println);
}
java-stream流用法
于 2023-11-01 16:54:23 首次发布