java8 笔记 - 方法引用(四)

方法引用

方法引用,不是方法调用

方法引用,不是方法调用

方法引用,不是方法调用

概念

Note that instances of functional interfaces can be created with
lambda expressions, method references, or constructor references.

函数式接口的实例可以通过 lambda 表达式、 方法引用、构造方法引用来创建。方法引用是 lambda 表达式的语法糖,任何使用方法引用的地方都可以由lambda表达式替换,但是并不是所有的lambda表达式都可以用方法引用来替换。

举例:

这就是一个打印集合所有元素的例子,value -> System.out.println(value) 是一个Consumer函数式接口,
这个函数式接口可以通过方法引用来替换。

public class LambdaTest {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("hello", "world", "welcome");

        list.forEach(value -> System.out.println(value));

    }
}
输出:
hello
world
welcome

使用方法引用的方式,和上面的输出是一样的,方法引用使用的是双冒号(::)

public class LambdaTest {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("hello", "world", "welcome");

        list.forEach(System.out::println);

    }
}
输出:
hello
world
welcome

分类

类别使用形式
静态方法引用类名 :: 静态方法名
实例方法引用对象名(引用名) :: 实例方法名
类方法引用类名 :: 实例方法名
构造方法引用类名 :: new

静态方法引用

public class Apple {

    private String category;
    private String color;
    private double weight;

    public Apple(String category, String color, double weight) {
        this.category = category;
        this.color = color;
        this.weight = weight;
    }

    public static int compareByWeight(Apple a1, Apple a2) {
        double diff = a1.getWeight() - a2.getWeight();
        return new Double(diff).intValue();
    }

    //getter setter toString
}    

有一个苹果的List,现在需要根据苹果的重量进行排序。List 的 sort 函数接收一个 Comparator 类型的参数,Comparator 是一个函数式接口,接收两个参数,返回一个int值。

Apple的静态方法compareByWeight正好符合Comparator函数式接口,所以可以使用:

Apple::compareByWeight 静态方法引用来替代lambda表达式

public class LambdaTest {

    public static void main(String[] args) {

        Apple apple1 = new Apple("红星", "Red", 280);
        Apple apple2 = new Apple("黄元帅", "Yello", 470);
        Apple apple3 = new Apple("红将军", "Red", 320);
        Apple apple4 = new Apple("国光", "Green", 300);

        List<Apple> appleList = Arrays.asList(apple1, apple2, apple3, apple4);

        //lambda 表达式形式
        //appleList.sort((Apple a1, Apple a2) -> {
        //    return new Double(a1.getWeight() - a2.getWeight()).intValue();
        //});

        //静态方法引用形式
        appleList.sort(Apple::compareByWeight);

        appleList.forEach(apple -> System.out.println(apple));

    }
}
输出:
Apple{category='红星', color='Red', weight=280.0}
Apple{category='国光', color='Green', weight=300.0}
Apple{category='红将军', color='Red', weight=320.0}
Apple{category='黄元帅', color='Yello', weight=470.0}

Apple.compareByWeight是方法的调用,而Apple::compareByWeight方法引用,这两者完全不是一回事。

实例方法引用

这个compareByWeight是一个实例方法

public class AppleComparator {

    public int compareByWeight(Apple a1, Apple a2) {
        double diff = a1.getWeight() - a2.getWeight();
        return new Double(diff).intValue();
    }
}

下面的例子通过实例对象的方法引用 comparator::compareByWeight 来代替lambda表达式

public class LambdaTest {

    public static void main(String[] args) {

        Apple apple1 = new Apple("红星", "Red", 280);
        Apple apple2 = new Apple("黄元帅", "Yello", 470);
        Apple apple3 = new Apple("红将军", "Red", 320);
        Apple apple4 = new Apple("国光", "Green", 300);


        List<Apple> appleList = Arrays.asList(apple1, apple2, apple3, apple4);

        //lambda 表达式形式
        //appleList.sort((Apple a1, Apple a2) -> {
        //    return new Double(a1.getWeight() - a2.getWeight()).intValue();
        //});

        //实例方法引用
        AppleComparator comparator = new AppleComparator();
        appleList.sort(comparator::compareByWeight);

        appleList.forEach(apple -> System.out.println(apple));

    }
}
输出:
Apple{category='红星', color='Red', weight=280.0}
Apple{category='国光', color='Green', weight=300.0}
Apple{category='红将军', color='Red', weight=320.0}
Apple{category='黄元帅', color='Yello', weight=470.0}

通过上面两个例子可以看到,静态方法引用和实例方法引用都是比较好理解的。

类方法引用

类方法引相对来说,不太容易理解。第一个例子中,Apple 的 compareByWeight 是一个静态方法,并且参数是两个。这个例子只是为了说明静态方法引用(其实,这个静态方法,放到哪个类都一样,放到Apple类里主要是为了区分与类方法引用的区别)

public class Apple {

    private String category;
    private String color;
    private double weight;

    public Apple(String category, String color, double weight) {
        this.category = category;
        this.color = color;
        this.weight = weight;
    }

    public static int compareByWeight(Apple a1, Apple a2) {
        double diff = a1.getWeight() - a2.getWeight();
        return new Double(diff).intValue();
    }

    //getter setter toString
} 

一般来说,同类型对象的比较,应该当前调用方法的对象与另外一个对象进行比较,好的设计应该像下面:

public class Apple {

    private String category;
    private String color;
    private double weight;

    public Apple(String category, String color, double weight) {
        this.category = category;
        this.color = color;
        this.weight = weight;
    }

    public int compareByWeight(Apple other) {
        double diff = this.getWeight() - other.getWeight();
        return new Double(diff).intValue();
    }

    //getter setter toString
}

还是之前List排序的例子,看看使用类方法引用如何写:

public class LambdaTest {

    public static void main(String[] args) {

        Apple apple1 = new Apple("红星", "Red", 280);
        Apple apple2 = new Apple("黄元帅", "Yello", 470);
        Apple apple3 = new Apple("红将军", "Red", 320);
        Apple apple4 = new Apple("国光", "Green", 300);


        List<Apple> appleList = Arrays.asList(apple1, apple2, apple3, apple4);

        //lambda 表达式形式
        //appleList.sort((Apple a1, Apple a2) -> {
        //    return new Double(a1.getWeight() - a2.getWeight()).intValue();
        //});

        //类方法引用
        appleList.sort(Apple::compareByWeight);

        appleList.forEach(apple -> System.out.println(apple));

    }
}
输出:
Apple{category='红星', color='Red', weight=280.0}
Apple{category='国光', color='Green', weight=300.0}
Apple{category='红将军', color='Red', weight=320.0}
Apple{category='黄元帅', color='Yello', weight=470.0}

这里使用的是:类名::实例方法名。首先要说明的是,方法引用不是方法调用。compareByWeight一定是某个实例调用的,就是lambda表达式的第一个参数,然后lambda表达式剩下的参数作为
compareByWeight的参数,这样compareByWeight正好符合lambda表达式的定义。

或者也可以这样理解:

(Apple a1, Apple a2) -> { return new Double(a1.getWeight() - a2.getWeight()).intValue(); }

int compareByWeight(Apple other) 需要当前对象调用,然后与另外一个对象比较,并且返回一个int值。可以理解为lambda表达式的第一个参数 a1 赋值给当前对象, 然后 a2 赋值给 other对象,然后返回int值。

构造方法引用

例子1:

public class ConstructionMethodTest {

    public String getString(Supplier<String> supplier) {
        return supplier.get();
    }

    public static void main(String[] args) {

        ConstructionMethodTest test = new ConstructionMethodTest();

        //lambda表达式形式
        System.out.println(test.getString(() -> { return new String();}));

        //构造方法引用形式
        System.out.println(test.getString(String::new));

    }
}

getString 方法接收一个Supplier类型的参数,Supplier 不接收参数,返回一个String。lambda表达式应该这样写:

() -> { return new String();}

替换成方法引用的形式如下: 实际上调用的是String 无参构造方法。

String::new

例子2:

public class ConstructionMethodTest {

    public String convert(String src, Function<String, String> function) {
        String dest = function.apply(src);

        return dest;
    }

    public static void main(String[] args) {

        ConstructionMethodTest test = new ConstructionMethodTest();

        //lambda表达式形式
        System.out.println(test.convert("hello", (String str) -> { return str; }));

        //构造方法引用形式
        System.out.println(test.convert("hello", String::new));

    }
}
输出:
hello
hello

convert 方法接收两个参数,一个字符串,一个Function,Functin接收一个字符串,返回一个字符串。Function 对应的lambda表达式应该这样写:

(String str) -> { return str;}

因为String 有一个构造方法,接收一个参数,返回一个参数

    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

所以可以用构造用方法引用的形式替换lambda表达式:

String::new
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值