stream

发布于:2023-01-22 ⋅ 阅读:(291) ⋅ 点赞:(0)

       Student student = new Student(1,"lemon",18,90.0, LocalDate.of(2000,01,01));
        Student student1 = new Student(2,"rtemo",28,60.0, LocalDate.of(2000,02,01));
        Student student2 = new Student(3,"wemon",38,60.0, LocalDate.of(2000,03,01));
        Student student3 = new Student(4,"semon",48,70.0, LocalDate.of(2000,04,01));
        Student student4 = new Student(5,"",58,80.0, LocalDate.of(2000,05,01));

        ArrayList<Student> studentArrayList = new ArrayList<>();
        studentArrayList.add(student);
        studentArrayList.add(student1);
        studentArrayList.add(student2);
        studentArrayList.add(student3);
        studentArrayList.add(student4);
//查学生分数大于60,且年龄大于39
  List<Student> collect = studentArrayList.stream().filter(s -> s.getScore() > 60 && s.getAge() > 38).collect(Collectors.toList());
//查最小值
//        List<Student> collect = studentArrayList.stream().sorted((s1, s2) -> s1.getAge() - s2.getAge()).limit(1).collect(Collectors.toList());
//        获取最小
//        Optional<Student> min = studentArrayList.stream().min(Comparator.comparing(p -> p.getScore()));
//查询平均分数
//        Double collect = studentArrayList.stream().collect(Collectors.averagingDouble(s -> s.getScore()));
//查询名字
//        List<String> collect = studentArrayList.stream().map(s -> s.getName()).collect(Collectors.toList());
//        查询名字为lemon的学生
//        List<Student> lemon = studentArrayList.stream().filter(s -> s.getName().equals("lemon")).collect(Collectors.toList());
        //查找分数为60的学生个数
//        long count = studentArrayList.stream().filter(s -> s.getScore() == 60).count();
        //查看分数为60的学生姓名
//        List<String> collect = studentArrayList.stream().filter(s -> s.getScore() == 60).map(s -> s.getName()).collect(Collectors.toList());
        //查询名字为w开头的
//        List<Student> collect = studentArrayList.stream().filter(s -> s.getName().startsWith("w")).collect(Collectors.toList());
        //        Map<Integer, String> collect = studentArrayList.stream().collect(Collectors.toMap(s -> s.getId(), s -> s.getName()));
//
//        collect.forEach((k,v) -> System.out.println(k +","+ v));

//输出所有及格的学生信息
stream.filter(s->{
    return s.getScore()>=60;
}).forEach(System.out::println);

//输出集合中的前三个元素

list.stream().sorted((s1,s2)->s2.getAge()-s1.getAge()).limit(3).forEach(System.out::println);

//需求:输出集合中所有的学生的姓名
list.stream().map(name->name.substring(0, 1)).forEach(System.out::println);;


//需求2:获取每个学生是否及格的信息
list.stream().map(s->s.getName() + (s.getScore()>=60?"及格":"不及格")).forEach(System.out::println);

//需求:计算所有学生总成绩
Optional<Integer> op = list.stream().map(Student::getScore).reduce((x,y)->x+y);
System.out.println(op.get());

filter():用来对容器中的数据进行过滤操作,要求传入一个断言型接口实例来描述过滤筛选的规则

limit(n):用来限定获取指定个数的前面元素

sorted():对容器中的数据进行排序,无参表示按照自然排序规则,也可以传入一个Compartor类型的实例进行定制排序

forEach(Consumer<T> con) 对处理的结果进行最终的循环操作

distinct():用来取出重复的记录  根据元素的hashCode和equauls

map():调用时需要传递一个Function类型的对象,根据集合中的每个元素映射一个结果,这个方法一般是用来提取或者转换信息的

- forEach()
- allMatch:检查集合中元素是否都匹配
- anyMatch:检查是否至少有一个匹配元素
- findFirst()返回第一个元素
- max()  获取最大的
- count() 计数
- collect()接收一个Collector接口收集器参数并将流处理的结果收集到指定的容器中
- Collectors工具类还支持集合的分组操作:

分组的两种方式:groupingBy   分组的过程中依据的是相等的比较 partitioningBy   分组的时候可以进行任何条件比较

本文含有隐藏内容,请 开通VIP 后查看