jdk8 方法引用

方法引用(Method reference.)

方法引用实际上是Lambda表达式的一种语法糖

  • 方法引用

    • 我们可以将方法引用看作是一个【函数指针】,function pointer.
  • 方法引用的分类

    • 类名::静态方法名

         public static void main(String[] args) {
      
              Student student1 = new Student("zhangsan", 10);
              Student student2 = new Student("lisi", 90);
              Student student3 = new Student("wangwu", 30);
              Student student4 = new Student("zhaoliu", 40);
      
              List<Student> students = Arrays.asList(student1, student2, student3, student4);
      
              students.sort((studentParam1, studentParam2) ->
                      Student.compareStudentBySocre(studentParam1, studentParam2));
      
              students.forEach(student -> System.out.println(student.getScore()));
      
              System.out.println("-----------");
              // 方法引用
              students.sort(Student::compareStudentByName);
              students.forEach(student -> System.out.println(student.getName()));
              
          }
      
    • 引用名(对象名称)::实例方法名称

          public static void main(String[] args) {
      
              Student student1 = new Student("zhangsan", 10);
              Student student2 = new Student("lisi", 90);
              Student student3 = new Student("wangwu", 30);
              Student student4 = new Student("zhaoliu", 40);
      
              List<Student> students = Arrays.asList(student1, student2, student3, student4);
              
      
              StudentComparator studentComparator = new StudentComparator();
      
              // 对象方法引用
              students.sort(studentComparator::compareStudentBySocre);
              students.forEach(student -> System.out.println(student.getScore()));
      
          }
      
    • 类名::实例方法名

          public static void main(String[] args) {
      
              Student student1 = new Student("zhangsan", 10);
              Student student2 = new Student("lisi", 90);
              Student student3 = new Student("wangwu", 30);
              Student student4 = new Student("zhaoliu", 40);
      
              List<Student> students = Arrays.asList(student1, student2, student3, student4);
      
              // 类名::实例方法名
      
              students.sort(Student::compareBySocre);
              students.forEach(student -> System.out.println(student.getScore()));
      
          }
      
    • 构造方法引用: 类名::new

      
      public class MetodReferenceTest {
      
      
          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) {
              
              
              MetodReferenceTest metodReferenceTest = new MetodReferenceTest();
              System.out.println(metodReferenceTest.getString(String::new));
              System.out.println(metodReferenceTest.getString2("hello", String::new));
      
          }
      }
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值