zoukankan      html  css  js  c++  java
  • Jdk8新特性之方法引用

    方法引用的格式

    符号表示 : ::

    符号说明 : 双冒号为方法引用运算符,而它所在的表达式被称为方法引用。
    应用场景 : 如果Lambda所要实现的方案 , 已经有其他方法存在相同方案,那么则可以使用方法引用。
    方法引用在JDK 8中使用方式相当灵活,有以下几种形式:

    1. instanceName::methodName 对象::实例方法
    2. ClassName::staticMethodName 类名::静态方法
    3. ClassName::methodName 类名::普通方法
    4. ClassName::new 类名::new 调用的构造器
    5. TypeName[]::new String[]::new 调用数组的构造器

    对象名 ::实例方法
    这是最常见的一种用法,与上例相同。如果一个类中已经存在了一个成员方法,则可以通过对象名引用成员方法,代码为:

    // 对象::实例方法
        @Test
        public void test01() {
            Date d = new Date();
            Supplier<Long> getTime = d::getTime;
            Long time = getTime.get();
            System.out.println(time);
        }

    方法引用的注意事项
    1. 被引用的方法,参数要和接口中抽象方法的参数一样
    2. 当接口抽象方法有返回值时,被引用的方法也必须有返回值

    类名 ::引用静态方法
    由于在 java.lang.System 类中已经存在了静态方法 currentTimeMillis ,所以当我们需要通过Lambda来调用该
    方法时,可以使用方法引用 , 写法是:

        // 类名::静态方法
        @Test
        public void test02() {
            //1585110320763
            Supplier<Long> timeMillis = System::currentTimeMillis;
            System.out.println(timeMillis.get());
        }

    类名 ::引用实例方法
    Java面向对象中,类名只能调用静态方法,类名引用实例方法是有前提的,实际上是拿第一个参数作为方法的调用者。

        // 类名::实例方法
        @Test
        public void test03() {
            //1
            Function<String, Integer> length = String::length;
            System.out.println(length.apply("hello"));   // 5
    
            Function<String, Integer> length1 = s1 -> {
                return s1.length();
            };
    
            //2
            BiFunction<String, Integer, String> substring = String::substring;
            String s = substring.apply("hello", 3);  //lo
            System.out.println(s);
    
            BiFunction<String, Integer, String> substr = (String s1, Integer i1) -> {
                return s1.substring(i1);
            };
            String hello = substr.apply("hello", 3);
            System.out.println(hello);
        }

    类名 ::new引用构造器
    由于构造器的名称与类名完全一样。所以构造器引用使用 类名称 ::new 的格式表示。首先是一个简单的 Person 类:

    // 类名::new引用类的构造器
        @Test
        public void test04() {
            Supplier<Person> p1 = Person::new;
            Person person = p1.get();
            System.out.println(person);
            BiFunction<String, Integer, Person> p2 = Person::new;
            Person wgr = p2.apply("wgr", 27);
            System.out.println(wgr);
        }

    数组 ::new 引用数组构造器
    数组也是 Object 的子类对象,所以同样具有构造器,只是语法稍有不同。

     // 类型[]::new
        @Test
        public void test05() {
            Function<Integer, int[]> f1 = int[]::new;
    
            int[] arr1 = f1.apply(10);
            System.out.println(Arrays.toString(arr1));
        }
  • 相关阅读:
    Cassandra 分页 读取数据
    cassandra高级操作之索引、排序以及分页
    cassandra 可视化工具
    SpringBoot集成Cassandra参考文章
    022 android studio 首次启动时默认的sdk安装路径
    021 Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout)
    020 本机Gradle目录 Could not find com.android.tools.build:gradle:4.1
    019 Android Studio打开XML文件Design显示Waiting for build to finish
    018 Could not download espresso-core-3.2.0.aar (androidx.test.espresso:espresso-core:3.2.0)
    017 Android Studio is using the following JDK location when running Gradle:
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12565517.html
Copyright © 2011-2022 走看看