lambda表达式和方法引用

lambda表达式和方法引用是孪生兄弟,方法引用的使用在很多情况下简化了lambda表达式。

方法引用符  ::

由lambda表达式简化得到方法引用,体验方法引用:

 1 package cn.ftf.add;
 2 
 3 public class PrintableDemo {
 4     public static void main(String[] args) {
 5         //lambda表达式
 6         print(s->System.out.println(s));
 7         //方法引用
 8         print(System.out::println);
 9     }
10     static void print(Printable p) {
11         p.printString("hello word!");
12     }
13 }
14 
15 interface Printable {
16     void printString(String s);
17 
18 }

引用类方法:

 1 package cn.ftf.add;
 2 /**
 3  * 引用类方法,例将字符串通过Integer.parseInt()转化为大写字符串
 4  * @author 房廷飞
 5  *
 6  */
 7 public class ConverterDemo {
 8     public static void main(String[] args) {
 9         useConverter(Integer::parseInt);
10     }
11     static void useConverter(Converter con) {
12         int a=con.convert("34");
13         System.out.println(a);
14     }
15 }
16 
17 interface Converter {
18     int convert(String a);
19 
20 }

引用对象的实例方法:

 1 package cn.ftf.add;
 2 /*
 3  * 引用对象的实例方法
 4  */
 5 public class PrinterDemo {
 6     public static void main(String[] args) {
 7         //使用lambda表达式
 8         usePrinter(s->System.out.println(s.toUpperCase()));
 9         //使用对象的实例方法
10         PrintString pr=new PrintString();
11         usePrinter(pr::printUpper);
12         
13         //如果方法是静态的就直接类::方法名
14         usePrinter(PrintString::printUpper1);
15     }
16     static void usePrinter(Printer pr){
17         pr.printUpperCase("hello word!");
18     }
19 }
20 
21 interface Printer {
22     void printUpperCase(String a);
23 }
24 
25 class PrintString {
26     public void printUpper(String str) {
27         System.out.println(str.toUpperCase());
28     }
29     static void printUpper1(String str) {
30         System.out.println(str.toUpperCase());
31 
32     }
33 
34 }

引用类的实例方法:

 1 package cn.ftf.add;
 2 /*
 3  * 引用类的实例方法
 4  */
 5 public class MyStringDemo {
 6     public static void main(String[] args) {
 7         //使用lambda表达式
 8         useMyString((str,a,b)->str.substring(a, b));
 9         //使用类中的实例方法
10         useMyString(String::substring);
11         //lambda表达式被类的实例方法替代的时候,第一个参数最为调用者,后面的参数作为方法参数,如上例    
12     }
13     static void useMyString(MyString mys) {
14         String str=mys.mySubString("hello", 2 ,4);
15         System.out.println(str);
16     }
17 }
18 interface MyString{
19     String mySubString(String str,int a,int b);
20 }

引用构造器:

 1 package cn.ftf.add;
 2 /*
 3  * 引用构造器
 4  */
 5 public class StudentDemo {
 6     public static void main(String[] args) {
 7         //lambda表达式
 8         useStudentBuilder((s,a)->new Student(s, a));
 9         //引用构造器
10         useStudentBuilder(Student::new);
11         //将参数全部传到构造器中
12     }
13     private static void useStudentBuilder(StudentBuilder stu) {
14         Student s= stu.build("小飞", 20);
15         System.out.println(s.getName()+"  "+s.getAge());
16         
17     }
18 
19 }
20 
21 
22 class Student{
23     public Student(String name, int age) {
24         super();
25         this.name = name;
26         this.age = age;
27     }
28     String name;
29     int age;
30     public String getName() {
31         return name;
32     }
33     public void setName(String name) {
34         this.name = name;
35     }
36     public int getAge() {
37         return age;
38     }
39     public void setAge(int age) {
40         this.age = age;
41     }
42     
43 }
44 interface StudentBuilder{
45     Student build(String name,int age);
46 }

猜你喜欢

转载自www.cnblogs.com/fangtingfei/p/11275737.html