Stream(四)

public class Test {
    /*
     * 创建:一步
     * 中间:0~n步
     * 终结:一步
     *
     * 三、终结操作
     * 1、void forEach(Consumer ):遍历流中的数据
     * 2、long count():统计个数
     * 3、boolean allMatch(Predicate p):是否全部满足xx条件
     *    boolean anyMatch(Predicate p):是否有一个满足xx条件
     *    boolean noneMatch(Predicate  p):是否全部都不满足xx条件
     * 4、Optional<T> findFirst():返回第一个
     *    Optional<T> findAny():返回任意一个
     * 5、Optional<T> max(Comparator c):找出最大的
     *    Optional<T> min(Comparator c)  :找出最小的
     * 6、T reduce(T iden, BinaryOperator b) 通过反复的运算,留下最后一个结果
     *    Optional<U>  reduce(BinaryOperator b)
     * 7、R collect(Collector c):把流中的数据最后都收集到一起
     *    Collector接口
     *
     *   BiConsumer  抽象方法   void accept(R r,T t)
     */
    public static void main(String[] args) {

//        test01();
//        test02();
//        test03();
//        test04();
//        test05();
//        test06();
//        test07();
//        test08();
//        test09();
//        test10();
        test11();
    }

    public static void test01(){
        Stream.of(1,2,3,4,5)
                .forEach(System.out::println);
    }

    public static void test02(){
        long count = Stream.of(1,2,3,4,5)
                .count();
        System.out.println("count = " + count);
    }

    public static void test03(){
        boolean result = Stream.of(1,3,5,7,9)
                .allMatch(t -> t%2!=0);
        System.out.println(result);
    }

    public static void test04(){
        boolean result = Stream.of(1,3,5,7,9)
                .anyMatch(t -> t%2==0);
        System.out.println(result);
    }

    public static void test05(){
        Optional<Integer> opt = Stream.of(1,3,5,7,9).findFirst();
        System.out.println(opt);
    }

    public static void  test06(){
        Optional<Integer> opt = Stream.of(1,2,3,4,5,7,9)
                .filter(t -> t%3==0)
                .findFirst();
        System.out.println(opt);
    }

    public static void  test7(){
        Optional<Integer> opt = Stream.of(1,2,4,5,7,8)
                .filter(t -> t%3==0)
                .findFirst();
        System.out.println(opt);
    }

    public static void  test8(){
        Optional<Integer> max = Stream.of(1,2,4,5,7,8)
                .max((t1,t2) -> Integer.compare(t1, t2));
        System.out.println(max);
    }

    public static void  test9(){
        Integer reduce = Stream.of(1,2,4,5,7,8)
                .reduce(0, (t1,t2) -> t1+t2);//BinaryOperator接口   T apply(T t1, T t2)
        System.out.println(reduce);
    }

    public static void  test10(){
        Optional<Integer> reduce = Stream.of(1,2,4,5,7,8)
                .reduce((t1,t2) -> t1>t2?t1:t2);//BinaryOperator接口   T apply(T t1, T t2)
        System.out.println(reduce);
    }

    public static void  test11(){
        List<Integer> list = Stream.of(1,2,4,5,7,8)
                .filter(t -> t%2==0)
                .collect(Collectors.toList());

        System.out.println(list);
    }

}

 

posted @ 2020-03-14 13:11  常温的冰  阅读(135)  评论(0)    收藏  举报