java8方法引用

java8方法引用

方法的引用就是java8中增加的一种语法糖

方法引用的形式

方法引用的标准形式是:类名::方法名。

有以下四种形式的方法引用:

引用静态方法 ContainingClass::staticMethodName
引用某个对象的实例方法 containingObject::instanceMethodName
引用某个类型的任意对象的实例方法 ContainingType::methodName
引用构造方法 ClassName::new

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 coompareStudentByScore(Student student1,Student student2){
        return student1.getScore() - student2.getScore();
    }


    public static int coompareStudentByName(Student student1,Student student2){
        return student1.getName().compareToIgnoreCase(student2.getName());
    }

    public int compareScore(Student student){
        return this.score-student.getScore();
    }

}
public class StudentComparator {

    public  int coompareStudentByScore(Student student1,Student student2){
        return student1.getScore() - student2.getScore();
    }

    public  int coompareStudentByName(Student student1,Student student2){
        return student1.getName().compareToIgnoreCase(student2.getName());
    }

}
public class MethodReferenceTest {

    public String toString(Supplier<String> supplier){
        return supplier.get()+"test";
    }

    public String toString2(String str, Function<String,String> function){
        return function.apply(str);
    }


    public static void main(String[] args) {
        Student student1   = new Student("zhangsan",20);
        Student student2   = new Student("lisi",90);
        Student student3   = new Student("wangwu",50);
        Student student4   = new Student("zhaoliu",60);

        List<Student> students = Arrays.asList(student1,student2,student3,student4);
        //lambda形式
       students.sort((studentparam1,studentparam2) -> Student.coompareStudentByName(studentparam1,studentparam2));
        students.forEach(student -> System.out.println(student.getName()));*/
       // 引用静态方法
       students.sort( Student::coompareStudentByName);
        students.forEach(student -> System.out.println(student.getName()));

        System.out.println("----------------------------------------------");

        //引用某个对象的实例方法
        StudentComparator studentComparator = new StudentComparator();
        students.sort(studentComparator::coompareStudentByScore);
        students.forEach(student -> System.out.println(student.getScore()));

        System.out.println("----------------------------------------------");

        //引用某个类型的任意对象的实例方法
        students.sort(Student::compareScore);
        students.forEach(student -> System.out.println(student.getScore()));
        MethodReferenceTest methodReferenceTest =  new MethodReferenceTest();
        //引用构造方法
        System.out.println( methodReferenceTest.toString(String::new));
        System.out.println( methodReferenceTest.toString2("hello",String::new));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值