Lambda Expression - Method Reference

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12807844.html

Examples of lambdas and method reference equivalents

There are three main kinds of method references:

1. A method reference to a static method

(for example, the method parseInt of Integer, written Integer::parseInt)

2. A method reference to an instance method of an arbitrary type

(for example, the method length of a String, written String::length)

3. A method reference to an instance method of an existing object

(for example, suppose you have a local variable expensiveTransaction that holds an object of type Transaction, which supports an instance method getValue; you can write expensiveTransaction::getValue)

Demo

MethodReferenceTest.java

 1 package org.fool.java8;
 2 
 3 import java.util.function.BiFunction;
 4 import java.util.function.BiPredicate;
 5 import java.util.function.Function;
 6 import java.util.function.Supplier;
 7 
 8 public class MethodReferenceTest {
 9     public static void main(String[] args) {
10         /*
11          * A method reference to a static method
12          */
13         Function<String, Integer> function1 = Integer::parseInt;
14         Integer result1 = function1.apply("123");
15         System.out.println(result1);
16 
17         /*
18          * A method reference to an instance method of an arbitrary type
19          */
20         BiPredicate<String, String> predicate = String::endsWith;
21         boolean result2 = predicate.test("hello", "world");
22         System.out.println(result2);
23 
24         /*
25          * A method reference to an instance method of an existing object
26          */
27         Person person = new Person("zhangsan", 18);
28         Supplier<String> result3 = person::getName;
29         System.out.println(result3.get());
30 
31         // BiFunction 只接收 2 个参数
32         BiFunction<String, Integer, Person> function = Person::new;
33         Person person1 = function.apply("zhangsan", 18);
34         System.out.println(person1);
35 
36         // TriFunction 自定义实现接收 3 个参数
37         TriFunction<String, Integer, String, Person> triFunction = Person::new;
38         Person person2 = triFunction.apply("lisi", 28, "female");
39         System.out.println(person2);
40     }
41 
42     @FunctionalInterface
43     public interface TriFunction<T, U, O, R> {
44         R apply(T t, U u, O o);
45     }
46 
47     private static class Person {
48         private String name;
49         private Integer age;
50         private String sex;
51 
52         public Person(String name, Integer age) {
53             this.name = name;
54             this.age = age;
55         }
56 
57         public Person(String name, Integer age, String sex) {
58             this.name = name;
59             this.age = age;
60             this.sex = sex;
61         }
62 
63         public String getName() {
64             return name;
65         }
66 
67         public void setName(String name) {
68             this.name = name;
69         }
70 
71         public Integer getAge() {
72             return age;
73         }
74 
75         public void setAge(Integer age) {
76             this.age = age;
77         }
78 
79         @Override
80         public String toString() {
81             return "Person{" +
82                     "name='" + name + '\'' +
83                     ", age=" + age +
84                     ", sex='" + sex + '\'' +
85                     '}';
86         }
87     }
88 }

Console Output

123
false
zhangsan
Person{name='zhangsan', age=18, sex='null'}
Person{name='lisi', age=28, sex='female'}

猜你喜欢

转载自www.cnblogs.com/agilestyle/p/12807844.html
今日推荐