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(流)。
阅读全文
0 0
- Java8函数式编程之五:方法引用详解
- Java8 Lambda表达式 函数式编程 方法引用
- java8函数式编程之五--lambda表达式与流
- 详解Java8特性之方法引用
- 【爱上Java8】Java8函数式方法的方法引用
- java8之方法引用
- Java8之方法引用
- java8之方法引用
- Java8之方法引用
- java8函数式编程之二:函数式接口的引用
- java8系列之方法引用
- Java8总结之函数式编程
- JAVA8之方法引用和构造引用
- java8新特性之函数式接口、lambda表达式、接口的默认方法、方法和构造函数的引用
- Java8 lambda表达式、函数式接口、方法引用
- Java8学习笔记(二)-函数式接口与方法引用
- JAVA8函数式编程
- java8 函数式编程
- java语言 基础总结
- RxJava响应式编程
- 2017.11.01【NOIP提高组】冲刺A组 总结
- 深入学习理解二叉搜索树(附详细讲解与实例分析)
- centos7安装最新版git_https://my.oschina.net/antsky/blog/514586
- Java8函数式编程之五:方法引用详解
- linux内核驱动基础
- win10解决pip安装慢的问题
- s3c2410_lcd & frame buffer 驱动分析 int __init s3c2410fb_probe(struct device *dev) { struct s3c2410f
- shell脚本监控tomcat服务运行状态,服务关闭后进行重启
- Hello
- linux 系统安装 redis
- easyui框架jsp父页面使用iframe标签嵌套子页面传值并加载data-grid数据问题
- java 基础 i++与++i的区别