Java8函数式编程之五:方法引用详解

来源:互联网 发布:卡通人物软件 编辑:程序博客网 时间:2024/06/05 19:22

方法引用详解:(method reference)
某些时候,Lambda表达式方法体的实现正好是已经存在的方法能够提供的功能,那么此时就可以用已经存在的方法替换Lambda表达式。
是Lambda的一种特殊情况,或者说是Lambda表达式的一种语法糖。

——————————————
Lambda表达式的语法与方法引用的语法对比:

1.比较1(Trade trade) -> trade.getMoney()      Trade :: getMoney

2.比较2

() -> Thread.currentThread().dumpStack()Thread.currrntThread() :: dumpStack

3.比较3

(str,i) -> str.substring(i)String :: substring

————————————————————————

实例1:

public class MethodReferenceDemo {    public static void main(String[] args){        List<String> list = Arrays.asList("hello","world","hello world");        //使用lambda表达式        list.forEach(item -> System.out.print(item));        //使用方法引用        list.forEach(System.out :: print);    }}

——————————————————

我们可以将方法引用分为4类:

1.类名 :: 静态方法名
2.引用名(对象名) :: 实例方法名
3.类名 :: 实例方法名
4.构造方法引用 类名 :: new

——————————————

实例1 : 类名 :: 静态方法名

public class Student {    private String name;    private int score;    public Student(String name, int score) {        this.name = name;        this.score = score;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    //写两个静态方法    //根据分数进行比较    public static int compareStudentByScore(Student student1,Student student2){        return student1.getScore() - student2.getScore();    }    //根据名字进行比较    public  static int compareStudentByName(Student student1,Student student2){        return student1.getName().compareToIgnoreCase(student2.getName());    }}

__

public class MethodReferenceTest {    public static void main(String[] args){        Student student1 = new Student("zhangsan",44);        Student student2 = new Student("lisi",55);        Student student3 = new Student("wangwu",76);        Student student4 = new Student("zhanoliu",98);        List<Student> students = Arrays.asList(student1,student2,student3,student4);        //使用lambda表达式        students.sort((s1,s2) -> Student.compareStudentByScore(s1,s2));        students.forEach(student -> System.out.println(student.getScore()));        //使用方法引用        students.sort(Student :: compareStudentByScore);        students.forEach(student -> System.out.println(student.getScore()));    }}

————————————————

实例2 :引用名(对象名) :: 实例方法名

public class StudentComparator {    //根据分数进行比较    public  int compareStudentByScore(Student student1,Student student2){        return student1.getScore() - student2.getScore();    }    //根据名字进行比较    public   int compareStudentByName(Student student1,Student student2){        return student1.getName().compareToIgnoreCase(student2.getName());    }}

——————

public class MethodReferenceTest {    public static void main(String[] args){        Student student1 = new Student("zhangsan",44);        Student student2 = new Student("lisi",55);        Student student3 = new Student("wangwu",76);        Student student4 = new Student("zhanoliu",98);        List<Student> students = Arrays.asList(student1,student2,student3,student4);        StudentComparator comparator = new StudentComparator();        //使用lambda表达式        students.sort((s1,s2) -> comparator.compareStudentByScore(s1,s2));        students.forEach(student -> System.out.println(student.getScore()));        students.sort((s1,s2) -> comparator.compareStudentByName(s1,s2));        students.forEach(student -> System.out.println(student.getName()));        //使用方法引用        students.sort(comparator :: compareStudentByScore);        students.forEach(student -> System.out.println(student.getScore()));        students.sort(comparator :: compareStudentByName);        students.forEach(student -> System.out.println(student.getName()));    }}

——————————————————

实例3 : 类名 :: 实例方法名

public class Student {    private String name;    private int score;    public Student(String name, int score) {        this.name = name;        this.score = score;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getScore() {        return score;    }    public void setScore(int score) {        this.score = score;    }    //比上文更好的设计,使用当前的score与传进来的score进行比较    public int compareByScore(Student student){        return this.getScore() - student.getScore();    }    public int compareByName(Student student){        return this.getName().compareToIgnoreCase(student.getName());    }}————————————public class MethodReferenceTest {    public static void main(String[] args) {        Student student1 = new Student("zhangsan", 44);        Student student2 = new Student("lisi", 55);        Student student3 = new Student("wangwu", 76);        Student student4 = new Student("zhanoliu", 98);        List<Student> students = Arrays.asList(student1, student2, student3, student4);        students.sort(Student::compareByScore);        students.forEach(student ->    System.out.println(student.getScore()));    }}

————————————————————————

实例4 : 类名 :: new

public class MethodReferenceTest {    public String getString(Supplier<String> supplier){        return supplier.get() +"test";    }    public String getString2(String str , Function<String,String> function){        return function.apply(str);    }    public static void main(String[] args) {        Student student1 = new Student("zhangsan", 44);        Student student2 = new Student("lisi", 55);        Student student3 = new Student("wangwu", 76);        Student student4 = new Student("zhanoliu", 98);        List<Student> students = Arrays.asList(student1, student2, student3, student4);       MethodReferenceTest test = new MethodReferenceTest();       System.out.println(test.getString(String :: new));       System.out.println(test.getString2("hello",String ::new));    }}

————————————————

好的,方法引用的全部类型就是上面这么多了,总结来说,方法引用就是lambda表达式的一种语法糖而已。

下一篇博客开始,我们开始学习Java8中最重要的一个部分,也就是Stream(流)。

原创粉丝点击