Java Lambda从入门到精通十七 流的使用-API总结

到目前为止,流的使用简单应用基本上讲解完了,我们将之前几节的demo汇总一下,代码中基本包含的Stream API中的全部方法,在这里作为一个使用的API,供大家参考。

代码

import java.util.*;
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest8 {

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        String[] names = {"小明","小华","小志","小东","小李","小张","小王","小周","小吴","小郑"};
        for(int i=0; i<10; i++) {
            Student student = Student.builder()
                    .name(names[i])
                    .age(12+i/5)
                    .no(i+1)
                    .math(85d+i).build();
            list.add(student);
        }
        System.out.println("======初始化数据:======");
        list.forEach(System.out::println);

        boolean allMatch = list.stream().allMatch(x -> x.getMath() > 80);
        System.out.println("所有学生数学成绩是否都大于80分:"+allMatch);

        boolean anyMatch = list.stream().anyMatch(x -> x.getMath() > 95);
        System.out.println("所有学生数学成绩是否存在大于95分:"+anyMatch);

        long count = list.stream().count();
        System.out.println("统计流中数量:"+count);

        Stream<Integer> distinct = list.stream().map(Student::getAge).distinct();
        System.out.print("所有学生年龄去重:");
        distinct.forEach(x->System.out.print(x+"  "));
        System.out.println();

        Stream<Object> empty = Stream.empty();
        System.out.println("empty创建一个空流:");
        empty.forEach(System.out::println);

        Stream<Integer> of = Stream.of(1, 2, 3);
        System.out.print("of创建流:");
        of.forEach(System.out::print);
        System.out.println();

        long filter = list.stream().filter(x -> x.getName().equals("小华")).count();
        System.out.println("学生中名字叫【小华】的有几个人:"+filter);

        Optional<Student> findAny = list.stream().filter(x -> x.getName().equals("小华")).findAny();
        System.out.println("findAny-学生中名字是否有叫【小华】的:"+findAny.isPresent());

        Optional<Student> findFirst = list.stream().filter(x -> x.getName().equals("小华")).findFirst();
        System.out.println("findFirst-学生中名字是否有叫【小华】的:"+findFirst.isPresent());

        System.out.print("所有学生姓名生成新流:");
        list.stream().map(Student::getName).forEach(x->System.out.print(x + "  "));
        System.out.println();

        Optional<Double> max = list.stream().map(Student::getMath).reduce(Double::max);
        System.out.println("max-所有学生数学成绩最高分:" +max.get());

        Optional<Student> max2 = list.stream().max(Comparator.comparing(Student::getMath));
        System.out.println("max2-所有学生数学成绩最高分:" +max2.get().getMath());

        Optional<Student> min = list.stream().min(Comparator.comparing(Student::getMath));
        System.out.println("所有学生数学成绩最低分:" +min.get().getMath());

        double reduce = list.stream().mapToDouble(Student::getMath).reduce(0d, (x, y) -> x + y);
        System.out.println("所有学生数学成绩加和:" +reduce);

        double reduce2 = list.stream().mapToDouble(Student::getMath).reduce(Double::sum).getAsDouble();
        System.out.println("所有学生数学成绩加和:" +reduce2);

        System.out.println("iterate生成流");
        Stream.iterate(0, n ->n+2)
                .limit(10)
                .forEach(System.out::println);

        System.out.println("iterate以数据交换的方式生成斐波那契流");
        Stream.iterate(new int[]{0,1}, n ->{
            //这里是用原数组替换值的方式,不断递增向后生成数列
            int sum = n[0]+n[1];
            n[0] = n[1];
            n[1] = sum;
            return n;
        }).limit(10)
                .forEach(t -> System.out.println("(" + t[0] + "," + t[1] +")"));

        System.out.println("iterate以流的方式生成斐波那契流");
        Stream.iterate(new int[]{0,1}, n ->new int[]{n[1],n[1]+n[0]})
                .limit(10)
                .forEach(n -> System.out.println("(" + n[0] + "," + n[1] +")"));


        //generate
        System.out.println("generate生成流");
        IntStream.generate(new IntSupplier(){
            @Override
            public int getAsInt() {
                return 5;
            }
        }).limit(10)
                .forEach(System.out::println);
    }
}

输出结果大家可以自己运行代码,看一下效果。