ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Stream(三)

2020-03-14 13:01:58  阅读:143  来源: 互联网

标签:Stream void System println public out


public class Test08 {
    /*
     * 二、中间的加工操作
     * (1)filter(Predicate p):过滤
     * (2)distinct():去重
     * (3)limit(long maxSize):取有限的几个
     * (4)skip(long n):跳过n个
     * (5)peek(Consumer action)  接收Lambda表达式,对流中的每个数据执行Lambda体操作
     * (6)sorted():排序,按照自然排序
     *      sorted(Comparator com):排序,按照定制排序
     * (7)map(Function f):接收Lambda表达式,对流中的每个数据,执行Lambda体操作,返回新的数据构成新的流
     * (8)flatMap(Function f)
     *
     *    map(Function<? super T,? extends R> mapper)   map操作流中的把T对象变成R对象,由R对象构成新的流
     *    flatMap(Function<? super T,? extends Stream<? extends R>> mapper)
     *                     flatMap把流中的数据T对象压扁变成一个Stream,然后把一个个的Stream最后再合成一个大的Stream
     */
    public static void main(String[] args) {
//        test01();
//        test02();
//        test03();
//        test04();
//        test05();
//        test06();
//        test07();
//        test08();
//        test09();
//        test10();
//        test11();
        test12();
    }

    public static void test01() {
        //1、创建Stream
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

        //2、加工处理
        //过滤:filter(Predicate p)
        //把里面的偶数拿出来
        /*
         * filter(Predicate p)
         * Predicate是函数式接口,抽象方法:boolean test(T t)
         */
        stream = stream.filter(t -> t % 2 == 0);

        //3、终结操作:例如:遍历
        stream.forEach(System.out::println);
    }

    //连起来写
    public static void test02() {
        Stream.of(1, 2, 3, 4, 5, 6)
                .filter(t -> t % 2 == 0)
                .forEach(System.out::println);
    }

    //去重
    public static void test03() {
        Stream.of(1, 2, 3, 4, 5, 6, 2, 2, 3, 3, 4, 4, 5)
                .distinct()
                .forEach(System.out::println);
    }

    //取指定数量
    public static void test04() {
        Stream.of(1, 2, 3, 4, 5, 6, 2, 2, 3, 3, 4, 4, 5)
                .limit(3)
                .forEach(System.out::println);
    }

    //去重加过滤
    public static void test05() {
        Stream.of(1, 2, 2, 3, 3, 4, 4, 5, 2, 3, 4, 5, 6, 7)
                .distinct()  //(1,2,3,4,5,6,7)
                .filter(t -> t % 2 != 0) //(1,3,5,7)
                .limit(3)
                .forEach(System.out::println);
    }

    //跳过指定的位置
    public static void test06() {
        Stream.of(1, 2, 3, 4, 5, 6, 2, 2, 3, 3, 4, 4, 5)
                .skip(5)
                .forEach(System.out::println);
    }

    //跳过 去重 过滤
    public static void test07() {
        Stream.of(1, 2, 3, 4, 5, 6, 2, 2, 3, 3, 4, 4, 5)
                .skip(5)
                .distinct()
                .filter(t -> t % 3 == 0)
                .forEach(System.out::println);
    }


    public static void test08() {
        long count = Stream.of(1, 2, 3, 4, 5, 6, 2, 2, 3, 3, 4, 4, 5)
                .distinct()
                .peek(System.out::println)  //Consumer接口的抽象方法  void accept(T t)
                .count();//终止操作
        System.out.println("count=" + count);
    }

    public static void test09() {
        //输出出前三个最大值,前三名最大的,不重复
        Stream.of(11, 2, 39, 4, 54, 6, 2, 22, 3, 3, 4, 54, 54)
                .distinct()
                .sorted((t1, t2) -> -Integer.compare(t1, t2))//Comparator接口  int compare(T t1, T t2) 注意这里前面加了个-号(-Integer)  倒序
                .limit(3)
                .forEach(System.out::println);
    }

    public static void test10() {
        Stream.of(1, 2, 3, 4, 5)
                .map(t -> t += 1)//Function<T,R>接口抽象方法 R apply(T t)
                .forEach(System.out::println);
    }

    //转换成大写
    public static void test11() {
        String[] arr = {"hello", "world", "java"};
        Arrays.stream(arr).map(t -> t.toUpperCase())
                .forEach(System.out::println);
    }

    public static void test12() {
        String[] arr = {"hello", "world", "java"};
        Stream<String> flatMap = Arrays.stream(arr)
                .flatMap(t -> Stream.of(t.split("|")));//Function<T,R>接口抽象方法 R apply(T t)  现在的R是一个Stream
        flatMap.forEach(System.out::println);
    }

}

 

标签:Stream,void,System,println,public,out
来源: https://www.cnblogs.com/hpdblogs/p/12491611.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有