Lambda Expression - Method Reference

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

 

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)

 

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'}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值