JDK1.8自学之路-10(lambda表达式实例方法的引用- str::endsWith)

练习

1.测试类

package com.citic._11instance_method_lambda;

import java.util.function.Function;

/**
 * lambda测试实例方法的引用
 *
 * @Package: com.citic._11instance_method_lambda
 * @ClassName: Test
 * @author: zq
 * @since: 2020/5/7 21:48
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class Test {

    public static void main(String[] args) {
        String str = "a.java";

        //匿名内部类
        Function fun1 = new Function() {
            @Override
            public Boolean apply(String suffix) {
                return str.endsWith(suffix);
            }
        };
        System.out.println(fun1.apply("java"));

        //lambda 常规写法
        Function function2 = suffix -> str.endsWith(suffix);
        System.out.println(function2.apply("java"));

        //lambda 简写
        Function function3 = str::endsWith;
        System.out.println(function3.apply("java"));
    }
}

 

你可能感兴趣的:(JDK1.8自学之路,lambda表达式,实例方法的引用)