reference methods and lambda expressions

reference methods and lambda expressions are twins, a reference method using a lambda expression is simplified in many cases.

:: character reference method

Get simplified method referenced by the expression lambda, experience method references:

 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 }

Reference class method:

. 1  Package cn.ftf.add;
 2  / ** 
. 3  * referenced class methods, for example by the Integer.parseInt string () string converted to uppercase
 . 4  * @author house fly Ting
 . 5  *
 . 6   * / 
. 7  public  class ConverterDemo {
 . 8      public  static  void main (String [] args) {
 . 9          useConverter (the parseInt :: Integer);
 10      }
 . 11      static  void useConverter (CON Converter) {
 12 is          int A = con.convert ( "34 is" );
 13 is          the System.out. the println (A);
 14      }
15 }
16 
17 interface Converter {
18     int convert(String a);
19 
20 }

Examples of the method referenced object:

. 1  Package cn.ftf.add;
 2  / * 
. 3  * Examples of the method of the referenced object
 . 4   * / 
. 5  public  class PrinterDemo {
 . 6      public  static  void main (String [] args) {
 . 7          // use the lambda expression 
. 8          usePrinter (S- > System.out.println (s.toUpperCase ()));
 . 9          // use the object's method of example 
10          PrintString at PR = new new PrintString at ();
 . 11          usePrinter (PR :: printUpper);
 12 is          
13 is          // If the method is static direct class method name :: 
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 }

Reference to an instance of the class method:

 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 }

 

Guess you like

Origin www.cnblogs.com/fangtingfei/p/11275737.html