zoukankan      html  css  js  c++  java
  • Stream流、方法引用

    Stream流定义

    对数组、集合或其他进行流式处理的一个接口,是目前Lambda表达式的使用者之一。

    方法引用定义

    双冒号::为引用运算符,而它所在的表达式被称为方法引用。Lambda要表达的函数方案已经存在于某个方法的实现中,那么则可以通过双冒号来引用该方法作为Lambda的替代者,是Lambda表达式的升级者。

    Stream流的使用

     public static void main(String[] args) {
            List<Camel> stream01 = new ArrayList<Camel>() {
                {
                    //元素序列-----翻译过来就是集合中的所有元素对象
                    add(new Camel("Elsdon", "Jaycob", "Java programmer", "male", 2000, 18));
                    add(new Camel("Tamsen", "Brittany", "Java programmer", "female", 2371, 55));
                    add(new Camel("Floyd", "Donny", "Java programmer", "male", 3322, 25));
                    add(new Camel("Sindy", "Jonie", "Java programmer", "female", 35020, 15));
                    add(new Camel("Vere", "Hervey", "Java programmer", "male", 2272, 25));
                    add(new Camel("Maude", "Jaimie", "Java programmer", "female", 2057, 87));
                    add(new Camel("Shawn", "Randall", "Java programmer", "male", 3120, 99));
                    add(new Camel("Jayden", "Corrina", "Java programmer", "female", 345, 25));
                    add(new Camel("Palmer", "Dene", "Java programmer", "male", 3375, 14));
                    add(new Camel("Addison", "Pam", "Java programmer", "female", 3426, 20));
                }
            };
            List<Camel> stream02 = new ArrayList<Camel>() {
                {
                    //元素序列-----翻译过来就是集合中的所有元素对象
                    add(new Camel("Elsdon", "Jaycob", "Java programmer", "male", 2000, 18));
                    add(new Camel("Tamsen", "Brittany", "Java programmer", "female", 2371, 55));
                    add(new Camel("Floyd", "Donny", "Java programmer", "male", 3322, 25));
                    add(new Camel("Sindy", "Jonie", "Java programmer", "female", 35020, 15));
                    add(new Camel("Vere", "Hervey", "Java programmer", "male", 2272, 25));
                    add(new Camel("Maude", "Jaimie", "Java programmer", "female", 2057, 87));
                    add(new Camel("Shawn", "Randall", "Java programmer", "male", 3120, 99));
                    add(new Camel("Jayden", "Corrina", "Java programmer", "female", 345, 25));
                    add(new Camel("Palmer", "Dene", "Java programmer", "male", 3375, 14));
                    add(new Camel("Addison", "Pam", "Java programmer", "female", 3426, 20));
                }
            };
    
            List<String> listCollect = stream01.
                    //filter创建流对象
                    stream()
                    //filter进行过滤,获取年龄大于30岁的动物
                    .filter(e -> e.getAge() > 30).
                    //filter进行类型转换,将动物对象转为动物的所有名字
                            map(e -> e.getName()).
                    //filter进行终端操作,流转换,将流转换为List集合
                            collect(Collectors.toList());
            long count = listCollect.
                    //filter创建流对象
                    stream().
                    //filter进行跳跃,跳过前两个元素
                    skip(2).
                    //filter对流进行截取,只取用前3个
                    limit(3).
                    //filter对流进行统计,统计其元素个数
                    count();
            listCollect.
                    //filter创建流对象
                    stream().
                    //filter进行迭代,最后进行打印,这里迭代为内部迭代(他帮你实现迭代逻辑);iterator为外部迭代
                    forEach(System.out::println);
            //将两个流对象进行拼接组合
            Stream.concat(stream02.stream(),stream01.stream());
        }
    

    方法引用的使用

    //函数式接口
    public interface Printable {
    
        void print(String message);
    
    }
        public static void main(String[] args) {
           //使用方法引用 System.out 对象存在  println方法也存在
            print("HelloWorld", System.out::println);
        }
        /**
         * @author  zhuyang
         * @description 使用函数式接口作为方法的参数
         * @date 2021-04-18 19:44 
         * @param message: 
         * @param printable:   
         */
        public static void print(String message,Printable printable){
            printable.print(message);
        }
    

    总结:匿名内部类和函数式接口为Lambda铺路,Stream流作为Lambda的现实使用者,方法引用则简化Lambda表达式,作为Lambda表达式的终极版本。

    XFS
  • 相关阅读:
    [CF598E] Chocolate Bar
    [CF629D] Babaei and Birthday Cake
    [CF961D] Pair Of Lines
    [CF468B] Two Sets
    [CF767C] Garland
    [CF864E] Fire
    [CF578C] Weakness and Poorness
    [CF555B] Case of Fugitive
    [CF118E] Bertown roads
    [CF1301D] Time to Run
  • 原文地址:https://www.cnblogs.com/xiaofengshan/p/14671112.html
Copyright © 2011-2022 走看看