JDK8的新特性(四)— —方法引用

一、目标

  1. 方法引用的目的
  2. 了解方法引用的格式
  3. 了解常用的几种方法引用方式

二、化繁就简,方法引用代替冗余代码

1. 一个栗子
public class DemoMethodImpl {

    public static void main(String[] args) {
        DemoMethodImpl demo = new DemoMethodImpl();
        //调用m1方法,并实现lambda表达式业务代码
        /*Integer m1Result = demo.m1(f1 -> {
            return Integer.valueOf(f1);
        });*/

        //利用 方法引用 减少了冗余代码
        //通过指定stringToInt方法重写接口的抽象方法,调用接口的抽象方法就是调用该方法
        Integer m1Result = demo.m1(demo::stringToInt);
        System.out.println(m1Result.getClass() + ",m1Result = " + m1Result);
    }
    //普通方法,字符串转整数
    public Integer stringToInt(String numStr){
        return Integer.valueOf(numStr);
    }

    public Integer m1(Function<String, Integer> f1){
        return f1.apply("999");
    }
}

==运行结果==
class java.lang.Integer,m1Result = 999

如上所见,通过方法引用的方式,可以省略冗余的重复代码。

2. 方法引用的几种格式
  • 运算符号:::,有此符号所在的表达式被称为“方法引用”
  • 应用场景:如果lambda表达式所要实现的功能,已经有其他已存在的方法实现了该功能,则可以使用方法是用
  • 常见格式:
    • instanceName::methodName 对象::方法名
    • className::staticMethodName 类名::静态方法名
    • className::methodName 类名::方法名
    • className::new 类名::new(调用构造方法)
    • TypeName[]::new 调用数组的构造方法

三、几种常见格式的使用示例

1. instanceName::methodName 对象::方法名
public class DemoMethod1 {
    public static void main(String[] args) {
        int[] intArr = new int[]{1,2,3,4,5};
        DemoMethod1 demoMethod1 = new DemoMethod1();
        //利用方法引用 代替 lambda表达式
        int getmax = demoMethod1.getMaxLambda(demoMethod1::getmax, intArr);
        System.out.println("getmax = " + getmax);
    }

    //求数组中的最大值
    public Integer getmax(int[] ints){
        int max = ints[0];
        for (int anInt : ints) {
            if(anInt > max){
                max = anInt;
            }
        }
        return max;
    }

    //
    public int getMaxLambda(Function<int[], Integer> f1, int[] intArr){
        return f1.apply(intArr);
    }
}

==运行结果==
getmax = 5
2. className::staticMethodName 类名::静态方法名
public class DemoMethod2 {
    public static void main(String[] args) {
        //lambda表达式定义
        Supplier<Long> s1 = ()->{
          return System.currentTimeMillis();
        };

        //静态方法引用
        Supplier<Long> s2 = System::currentTimeMillis;

        Long aLong = s1.get();
        System.out.println("aLong = " + aLong);
        System.out.println("---------------------");
        Long aLong1 = s2.get();
        System.out.println("aLong1 = " + aLong1);
    }
}

==运行结果==
aLong = 1583745638473
---------------------
aLong1 = 1583745638474
3. className::methodName 类名::方法名

类名调用实例方法这种方式有一个前提:用第一个参数作为方法的调用者

public class DemoMethod3 {
    public static void main(String[] args) {
        //lambda表达式
        Function<String, Integer> f1 = (str)->{
            return str.length();
        };
        //类名::方法名
        Function<String, Integer> f2 = String::length;

        BiFunction<String, Integer, String> bf1 = String::substring;

        String s1 = "hello world";
        Integer apply1 = f1.apply(s1);
        System.out.println("apply1 = " + apply1);
        System.out.println("------------------");
        Integer apply2 = f2.apply(s1);
        System.out.println("apply2 = " + apply2);
        System.out.println("------------------");
        String apply3 = bf1.apply(s1, 4);
        System.out.println("apply3 = " + apply3);
    }
}

==运行结果==
apply1 = 11
------------------
apply2 = 11
------------------
apply3 = o world
4. className::new 类名::new(调用构造方法)
public class DemoMethod4 {

    public static void main(String[] args) {
        Supplier<Person> s1 = ()->{
          return new Person();
        };
        Supplier<Person> s2 = Person::new;
        Person person1 = s1.get();
        System.out.println("person1 = " + person1.toString());
        Person person2 = s2.get();
        System.out.println("person2 = " + person2.toString());

        System.out.println("----------------");
        BiFunction<String, Integer, Person> bi1 = (name, age)->{
          return new Person(name, age);
        };
        BiFunction<String, Integer, Person> bi2 = Person::new;
        Person p3 = bi1.apply("xiaoming", 13);
        System.out.println("p3 = " + p3.toString());
        Person p4 = bi2.apply("xiaohong", 14);
        System.out.println("p4 = " + p4.toString());
    }
}

class Person{
    private String name;
    private Integer age;

    public Person() {
    }

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    //getting、setting、toString
}

==运行结果==
person1 = Person{name='null', age=null}
person2 = Person{name='null', age=null}
----------------
p3 = Person{name='xiaoming', age=13}
p4 = Person{name='xiaohong', age=14}
5. TypeName[]::new 调用数组的构造方法
public class DemoMethod5 {
    public static void main(String[] args) {
        Function<Integer, int[]> f1 = (i)->{
            return new int[i];
        };
        Function<Integer, int[]> f2 = int[]::new;

        int[] apply = f1.apply(5);
        System.out.println("apply = " + apply);
        int[] apply1 = f2.apply(5);
        System.out.println("apply1 = " + apply1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值