Lambda 表达式

定义接口

interface MathOperation {
    int operation(int a, int b);
    default String fun(){
        return "Hello";
    }
}
 public static void test2(){
        MathOperation addition = (int a, int b) -> a + b;
        System.out.println(addition.operation(10,20));
        MathOperation addition2 = new MathOperation() {
            @Override
            public int operation(int a, int b) {
                return a+b;
            }
        };
        System.out.println(addition2.fun());
        System.out.println(addition2.operation(10,20));
        // 不用类型声明
        MathOperation subtraction = (a, b) -> a - b;
        // 大括号中的返回语句
        MathOperation multiplication = (int a, int b) -> { return a * b; };
        // 没有大括号及返回语句
        MathOperation division = (int a, int b) -> a / b;
    }
 public static int operate(int a, int b, MathOperation mathOperation){
        return mathOperation.operation(a, b);
    }
    public int add(int a,int b){
        return a+b;
    }
    public static void test1(){
        int operate1 = operate(10, 10, (a, b) -> a + b);
        System.out.println(operate1);
        int operate2 = operate(50, 50, new TestLambda()::add);
        System.out.println(operate2);
    }

方法引用

@FunctionalInterface
public interface Supplier<T> {
    T get();
}
class NodeRenderingHandler<N extends Object> {
    public NodeRenderingHandler(Class<N> aClass,  NodeRenderingHandler.CustomNodeRenderer<N> adapter) {

    }

    public void render(Object node, String s) {

    }

    public interface CustomNodeRenderer<N extends Object>  {
        void render(N var1, String var3);
    }
}

public class Tesr4 {
    public void test(){
        new NodeRenderingHandler<>(String.class, new NodeRenderingHandler.CustomNodeRenderer<String>() {
            @Override
            public void render(String var1, String var3) {

            }
        });
    }
    public void test1(){
        new NodeRenderingHandler<>(String.class, (var1, var3) -> {

        });
    }
    public void test2(){
        new NodeRenderingHandler<>(Integer.class, new NodeRenderingHandler.CustomNodeRenderer<Integer>() {
            @Override
            public void render(Integer var1, String var3) {
                
            }
        });
    }
}

参考:
https://www.runoob.com/java/java8-new-features.html