Lambda Expression - Method Reference

原创转载请注明出处:http://agilestyle.iteye.com/blog/2424849

 

Examples of lambdas and method reference equivalents

Lambda Expression - Method Reference_第1张图片

 

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)

 

e.g.

package org.fool.java8;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;

public class MethodReferenceTest {
    public static void main(String[] args) {
        /*
         * A method reference to a static method
         */
        Function function1 = Integer::parseInt;
        Integer result1 = function1.apply("123");
        System.out.println(result1);

        /*
         * A method reference to an instance method of an arbitrary type
         */
        BiPredicate predicate = String::endsWith;
        boolean result2 = predicate.test("hello", "world");
        System.out.println(result2);

        /*
         * A method reference to an instance method of an existing object
         */
        Person person = new Person("zhangsan", 18);
        Supplier result3 = person::getName;
        System.out.println(result3.get());

        /**
         * BiFunction只接收 2 个参数
         */
        BiFunction function = Person::new;
        Person person1 = function.apply("zhangsan", 18);
        System.out.println(person1);

        /**
         * TriFunction 自定义实现接收 3 个参数
         */
        TriFunction triFunction = Person::new;
        Person person2 = triFunction.apply("lisi", 28, "female");
        System.out.println(person2);
    }

    @FunctionalInterface
    public interface TriFunction {
        R apply(T t, U u, O o);
    }

    private static class Person {
        private String name;
        private Integer age;
        private String sex;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Person(String name, Integer age, String sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }
}

 Console Output

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

 

 

 

 

你可能感兴趣的:(FP)