【jdk8新特性】方法引用和构造器引用,包看包会

博客地址(点击即可访问)github源码地址
Lamda表达式https://github.com/zz1044063894/lambda
函数式接口https://github.com/zz1044063894/methodInterface
方法引用和构造器引用https://github.com/zz1044063894/qoute
Stream APIhttps://github.com/zz1044063894/StreamAPI
接口中的默认方法与静态方法https://github.com/zz1044063894/static-default-method
新时间日期APIhttps://github.com/zz1044063894/newDateTimeAPI

方法引用

概念

  • 方法引用通过方法的名字来指向一个方法。

  • 方法引用可以使语言的构造更紧凑简洁,减少冗余代码。

  • 方法引用使用一对冒号 ::

若lambda表达体中的内容有方法已经实现了,我们可以使用方法引用,还可以理解为,方法引用是lambda表达是的另一种表现形式

主要语法格式

  • 对象::实例方法
//对象::实例方法
    @Test
    public void test(){
        //
        Consumer<String> consumer = (x)-> System.out.println(x);
        
        PrintStream printStream = System.out;
        Consumer<String> consumer1 = printStream::println;

        consumer.accept("使用lambda表达式()->形式");
        consumer1.accept("使用引用方法,对象的实例方法");

    }

运行结果:
在这里插入图片描述

  • 类::静态方法
//类::静态方法
    @Test
    public void test1(){
        //未使用类::静态方法引用
        Comparator<Integer> comparator = (x,y) -> Integer.compare(x,y);

        //使用类::静态方法引用
        Comparator<Integer> comparator1 = Integer::compare;


        TreeSet<Integer> treeSet = new TreeSet<>(comparator);
        treeSet.add(1);
        treeSet.add(4);
        treeSet.add(2);

        TreeSet<Integer>   treeSet1 = new TreeSet<>(comparator1);
        treeSet1.add(1);
        treeSet1.add(4);
        treeSet1.add(2);
        System.out.println("使用lambda表达式()->形式:"+treeSet.toString());
        System.out.println("使用引用方法,类的静态方法:"+treeSet1.toString());

    }

运行结果:在这里插入图片描述

  • 类::实例方法
 //类::实例方法
    @Test
    public void test2() {
        //未使用类::实例方法引用
        BiPredicate<String, String> biPredicate = (x, y) -> x.equals(y);

        //使用类::实例方法引用
        BiPredicate<String, String> biPredicate2 = String::equals;

        System.out.println("使用lambda表达式()->形式:" + biPredicate.test("hellow", "hellow1"));
        System.out.println("使用引用方法,类的实例方法:" + biPredicate.test("hellow", "hellow1"));
    }

结果:在这里插入图片描述

注意:

  • 在lambda体中调用方法的参数类型和返回值类型必须和函数式接口中的参数类型和返回值类型保持一直
  • 若lambda参数列表中的第一个参数是方法的调用者时,第二个参数是实例方法的参数是,可以使用类名::实例方法

构造器引用

语法格式:

类名::new

使用方法

 @Test
    public void test(){
        //未使用构造器方法引用
        Supplier<String> supplier = ()->new String();



        //使用构造器方法引用
        Supplier<String> supplier1 = String::new;
        System.out.println("使用lambda表达式()->形式:" + supplier.get());
        System.out.println("使用引用方法,构造器方法:" + supplier1.get());
    }

运行结果:
在这里插入图片描述
分析:因为Supplier.get()方法是使用无参数的构造方法,所以调用的是无参的构造方法,String无参的构造方法默认值是“”字符串,所以输出都是空字符串。

 @Test
    public void test1(){
        //未使用构造器方法引用
        Function<String,String> function = (x)->new String(x);



        //使用构造器方法引用
        Function<String,String>  function1 = String::new;

        System.out.println("使用lambda表达式()->形式:" + function.apply("JingChu 努力学习"));
        System.out.println("使用引用方法,构造器方法:" + function1.apply("JingChu 努力学习"));
    }

结果:在这里插入图片描述
第二个例子中,我们使用了拥有一个参数的方法,所以可以正确输出,使用什么方法决定了调用什么样的构造器。

注意:

要调用的构造器参数列表要与函数式接口中抽象方法的参数和返回值类型保持一致

数组引用

数组引用和对象引用相似,格式为type[]::new;

@Test
    public void test() {
        //未使用数组引用
        Function<Integer, String[]> function = (x) -> new String[x];

        //使用数组引用
        Function<Integer, String[]> function1 = (x) -> new String[x];


        String[] strings = function.apply(10);
        System.out.println(strings.length);
        String[] strings1 = function.apply(20);
        System.out.println(strings1.length);


    }

结果:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值