---
title: "Lambda Expression - Method Reference"
author: "fairy_xzc"
date: "2018-06-12T21:23:39Z"
tags: []
categories: ["编程语言"]
slug: "lambda-expression-method-reference"
draft: false
lastmod: "2018-06-12T21:26:06Z"
id: 2424849
url: "https://agilestyle.iteye.com/blog/2424849"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2424849](/blog/2424849)**

 

Examples of lambdas and method reference equivalents

![](http://dl2.iteye.com/upload/attachment/0130/0514/5008443a-02b1-3367-bbcf-801ae12c28bf.png)

 

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<String, Integer> function1 = Integer::parseInt;
        Integer result1 = function1.apply("123");
        System.out.println(result1);

        /*
         * A method reference to an instance method of an arbitrary type
         */
        BiPredicate<String, String> 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<String> result3 = person::getName;
        System.out.println(result3.get());

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

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

    @FunctionalInterface
    public interface TriFunction<T, U, O, R> {
        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'}
```