码迷,mamicode.com
首页 > 编程语言 > 详细

Java 8 方法引用

时间:2018-12-02 14:56:28      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:tail   michael   create   特定   ret   code   链接   ati   art   

Java 8 方法引用

There are four kinds of method references:

Kind Example
Reference to a static method ContainingClass::staticMethodName
Reference to an instance method of a particular object containingObject::instanceMethodName
Reference to an instance method of an arbitrary object of a particular type ContainingType::methodName
Reference to a constructor ClassName::new

方法引用通过方法的名字来指向一个方法。
方法引用可以使语言的构造更紧凑简洁,减少冗余代码。
方法引用使用一对冒号 :: 。
下面,我们定义了 4 个方法作为例子来区分 Java 中 4 种不同方法的引用。

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

public class ComparisonProvider {

    public int compareByName(Person a, Person b) {
        return a.getName().compareTo(b.getName());
    }

    public static int compareByAge(Person a, Person b) {
        return a.getBirthday().compareTo(b.getBirthday());
    }

    public static void main(String[] args) {
        List<Person> roster = Person.createRoster();
        Person[] rosterAsArray = roster.toArray(new Person[roster.size()]);

        // 特定对象的实例方法引用: containingObject::instanceMethodName
        ComparisonProvider myComparisonProvider = new ComparisonProvider();
        System.out.println("排序前:" + Arrays.toString(rosterAsArray));
        Arrays.sort(rosterAsArray, myComparisonProvider::compareByName);
        System.out.println("排序后:" + Arrays.toString(rosterAsArray));

        // 静态方法引用:ContainingClass::staticMethodName
        System.out.println("排序前:" + Arrays.toString(rosterAsArray));
        Arrays.sort(rosterAsArray, ComparisonProvider::compareByAge);
        System.out.println("排序后:" + Arrays.toString(rosterAsArray));

        // 特定类型的任意对象的实例方法的引用: ContainingType::methodName
        String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" };
        System.out.println("排序前:" + Arrays.toString(stringArray));
        Arrays.sort(stringArray, String::compareToIgnoreCase);
        System.out.println("排序后:" + Arrays.toString(stringArray));

        // 构造器引用: ClassName::new
        Person newPerson = Person.create(Person::new);
        newPerson.setName("xiaocao");
        newPerson.setBirthday(LocalDate.of(1995, 8, 20));
        System.out.println(newPerson);
    }
}

点击查看结果

排序前:[[Fred, 1980-06-20], [Jane, 1990-07-15], [George, 1991-08-13], [Bob, 2000-09-12]]
排序后:[[Bob, 2000-09-12], [Fred, 1980-06-20], [George, 1991-08-13], [Jane, 1990-07-15]]
排序前:[[Bob, 2000-09-12], [Fred, 1980-06-20], [George, 1991-08-13], [Jane, 1990-07-15]]
排序后:[[Fred, 1980-06-20], [Jane, 1990-07-15], [George, 1991-08-13], [Bob, 2000-09-12]]
排序前:[Barbara, James, Mary, John, Patricia, Robert, Michael, Linda]
排序后:[Barbara, James, John, Linda, Mary, Michael, Patricia, Robert]
[xiaocao, 1995-08-20]

参考链接:

Java 8 方法引用

标签:tail   michael   create   特定   ret   code   链接   ati   art   

原文地址:https://www.cnblogs.com/hglibin/p/10053265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!