Stream 流中间操作
以下列出了在日常开发中常用的中间操作方法
有状态操作
distinct(去重)
List<String> list = Arrays.asList("a", "b", "c", "a", "b", "c");
list.stream().distinct().forEach(System.out::println);
// 输出:a b csorted(排序)
List<String> list = Arrays.asList("c", "a", "b");
list.stream().sorted().forEach(System.out::println);
// 输出:a b c
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.stream().sorted((i1, i2) -> i2 - i1).forEach(System.out::println);
// 输出:5 4 3 2 1List<Student> students = new ArrayList<>();
students.add(new Student("张三", 20));
students.add(new Student("李四", 18));
students.add(new Student("王五", 19));
students.add(new Student("赵六", 18));
// 按年龄升序,年龄相同按姓名升序
students.sort(Comparator.comparing(Student::getAge).thenComparing(Student::getName));
students.forEach(System.out::println);skip
limit
无状态
filter
map
flatMap
peek
最后更新于