Java8新特性之方法引用(三)

1. 方法引用介绍

方法引用其实是lambda表达式的升级写法,采用::固定语法,可以使代码更简洁、紧凑;

2. 方法引用的5种类型

2.1 对象引用::实例方法名

函数式接口抽象方法的形参列表与实例方法的一致,且方法的返回值类型一致;

public class MethodReferencesTest {

    public static void main(String[] args) {
        // 对象引用
        MethodReferencesTest methodReferencesTest = new MethodReferencesTest();
        // lambda表达式写法
        Integer r1 = MethodReferencesTest.getResult((a, b) -> methodReferencesTest.sum(a, b));
        System.out.println("r1:" + r1);
        // 方法引用(对象引用::实例方法名)写法
        Integer r2 = MethodReferencesTest.getResult(methodReferencesTest::sum);
        System.out.println("r2:" + r2);
    }

    public static Integer getResult(CalculateInterface calculateInterface) {
        return calculateInterface.calculate(123, 456);
    }

    /**
     * 实例方法
     */
    public Integer sum(Integer a, Integer b) {
        return a + b;
    }
}

@FunctionalInterface
interface CalculateInterface {
    /**
     * 函数式接口抽象方法
     */
    Integer calculate(Integer a, Integer b);
}

// 运行结果
r1:579
r2:579

2.2 类名::静态方法名

函数式接口抽象方法的形参列表与静态方法的一致,且方法的返回值类型一致;

public class MethodReferencesTest {

    public static void main(String[] args) {
        // lambda表达式写法
        Integer r1 = MethodReferencesTest.getResult((a, b) -> MethodReferencesTest.sum(a, b));
        System.out.println("r1:" + r1);
        // 方法引用(类名::静态方法名)写法
        Integer r2 = MethodReferencesTest.getResult(MethodReferencesTest::sum);
        System.out.println("r2:" + r2);
    }

    public static Integer getResult(CalculateInterface calculateInterface) {
        return calculateInterface.calculate(123, 456);
    }

    /**
     * 静态方法
     */
    public static Integer sum(Integer a, Integer b) {
        return a + b;
    }
}

@FunctionalInterface
interface CalculateInterface {
    /**
     * 函数式接口抽象方法
     */
    Integer calculate(Integer a, Integer b);
}

2.3 类名::实例方法名

函数式接口抽象方法的第一个参数是实例方法所在类的对象,剩下的参数(或无参)是实例方法的入参,且方法的返回值类型一致;

public class MethodReferencesTest {

    public static void main(String[] args) {
        // lambda表达式写法
        boolean r1 = MethodReferencesTest.getResult((a, b) -> a.equals(b));
        System.out.println("r1:" + r1);
        // 方法引用(类名::实例方法名)写法
        boolean r2 = MethodReferencesTest.getResult(CalculateEntity::equals);
        System.out.println("r2:" + r2);
    }

    public static Boolean getResult(CalculateInterface calculateInterface) {
        CalculateEntity a = new CalculateEntity(123);
        CalculateEntity b = new CalculateEntity(456);
        return calculateInterface.compare(a, b);
    }
}

class CalculateEntity {
    private Integer num;

    public CalculateEntity(Integer num) {
        this.num = num;
    }

    /**
     * 实例方法
     */
    public Boolean equals(CalculateEntity entity) {
        return this.num.equals(entity.num);
    }
}

@FunctionalInterface
interface CalculateInterface {
    /**
     * 函数式接口抽象方法
     */
    Boolean compare(CalculateEntity a, CalculateEntity b);
}

// 运行结果
r1:false
r2:false

2.4 类名::new

函数式接口抽象方法的形参列表与类的构造方法的一致,且抽象方法返回的类型是该类名的类型;

public class MethodReferencesTest {

    public static void main(String[] args) {
        // lambda表达式写法
        CalculateEntity r1 = MethodReferencesTest.getResult(num -> new CalculateEntity(num));
        System.out.println("r1:" + r1);
        // 方法引用(类名::new)写法
        CalculateEntity r2 = MethodReferencesTest.getResult(CalculateEntity::new);
        System.out.println("r2:" + r2);
    }

    public static CalculateEntity getResult(CalculateInterface calculateInterface) {
        return calculateInterface.generateEntity(123);
    }
}

class CalculateEntity {

    private Integer num;

    /**
     * 构造方法
     */
    public CalculateEntity(Integer num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "num = " + num;
    }
}

@FunctionalInterface
interface CalculateInterface {
    /**
     * 函数式接口抽象方法
     */
    CalculateEntity generateEntity(Integer num);
}

// 运行结果
r1:num = 123
r2:num = 123

2.5 类名[]::new

函数式接口抽象方法的参数是int类型或者Integer类型,表示数组长度,抽象方法的返回值类型是该类名类型的数组;

public class MethodReferencesTest {

    public static void main(String[] args) {
        // lambda表达式写法
        CalculateEntity[] r1 = MethodReferencesTest.getResult(length -> new CalculateEntity[length]);
        System.out.println("r1:" + Arrays.toString(r1));
        // 方法引用(类名[]::new)写法
        CalculateEntity[] r2 = MethodReferencesTest.getResult(CalculateEntity[]::new);
        System.out.println("r2:" + Arrays.toString(r2));
    }

    public static CalculateEntity[] getResult(CalculateInterface calculateInterface) {
        return calculateInterface.generateEntityArr(5);
    }
}

class CalculateEntity {

    private int num;

    @Override
    public String toString() {
        return "num = " + num;
    }
}

@FunctionalInterface
interface CalculateInterface {
    /**
     * 函数式接口抽象方法
     */
    CalculateEntity[] generateEntityArr(Integer length);
}

// 运行结果
r1:[null, null, null, null, null]
r2:[null, null, null, null, null]

来源:https://blog.csdn.net/gu19930914/article/details/115705383

posted @ 2023-10-12 17:57  Lafite-1820  阅读(4)  评论(0编辑  收藏  举报