Java8 MethodReference方法引用

package com.lgx.jdk8.part02;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 方法引用的學習
 */
public class Test02MethodReference {
    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) {
        List<String> list = Arrays.asList("hello", "world", "hello world");

        //之前學習Lambda的寫法
        list.forEach(item -> System.out.println("item = [" + item + "]"));

        //方法引用的寫法
        list.forEach(System.out::println);

        //方法引用共分爲4類:
        //a、類名::靜態方法名
        //當你使用的Lambda表達式恰好有一個靜態方法可以完成,則可以使用這種方式
        Student student1 = new Student("zhangsan", 60);
        Student student2 = new Student("lisi", 80);
        Student student3 = new Student("wangwu", 50);
        Student student4 = new Student("zhaoliu", 78);

        List<Student> studentList = Arrays.asList(student1, student2, student3, student4);
        //studentList.sort((studentPara1, studentPara2) -> Student.compareStudentsByScore(studentPara1, studentPara2));//傳統寫法
        /*studentList.sort(Student::compareStudentsByScore);
        studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]"));

        //b、引用名(對象名)::實例方法名
        /*studentList.sort(new StudentComparator()::compareStudentsByScore);
        studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]"));*/

        //c、類名::實例方法名
        //與上面那個類似,Lambda表達式傳遞進來的第一個參數作爲當前對象,當作調用者,
        //第二及以後的參數才傳遞給被調用的方法
        /*studentList.sort(Student::compareByScore);
        studentList.forEach(student -> System.out.println("student = [" + student.getScore() + "]"));
        studentList.sort(Student::compareByName);
        studentList.forEach(student -> System.out.println("student = [" + student.getName() + "]"));*/

        //拓展下
        List<String> cites = Arrays.asList("jiangxi", "hubei", "shenzhen", "beijing");
        //Collections.sort(cites, (city1, city2) -> city1.compareTo(city2));//傳統寫法
        //cites.forEach(city -> System.out.println("city = [" + city + "]"));//傳統寫法
        Collections.sort(cites, String::compareTo);//這裏是第3種寫法
        cites.forEach(System.out::println);//這裏是第2種寫法

        //d、類名::new
        //也叫構造方法引用
        Test02MethodReference test = new Test02MethodReference();
        String result = test.getString(String::new);
        System.out.println("result = [" + result + "]");
        String result2 = test.getString2("hello", String::new);
        System.out.println("result2 = [" + result2 + "]");

    }
}

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 compareStudentsByScore(Student student, Student student2){
        return student.getScore() - student2.getScore();
    }
    //按照學生的名字對學生進行排序
    public static int compareStudentsByName(Student student, Student student2){
        return student.getName().compareTo(student2.getName());
    }

    //按照學生的成績對學生進行排序
    public int compareByScore(Student student){
        return this.getScore() - student.getScore();
    }
    //按照學生的名字對學生進行排序
    public int compareByName(Student student){
        return this.getName().compareTo(student.getName());
    }
}

class StudentComparator{
    //按照學生的成績對學生進行排序
    public int compareStudentsByScore(Student student, Student student2){
        return student.getScore() - student2.getScore();
    }
    //按照學生的名字對學生進行排序
    public int compareStudentsByName(Student student, Student student2){
        return student.getName().compareTo(student2.getName());
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章