美文网首页
Java Lambda 表示式(箭头函数)

Java Lambda 表示式(箭头函数)

作者: Tinyspot | 来源:发表于2023-10-21 21:36 被阅读0次

1. 箭头操作符 (->)

语法:参数 -> 方法体

# 表达式风格
(parameters) -> expression
# 块风格
(parameters) -> { statements; }

2. 两个箭头

定义两个函数,第一个函数是函数定义函数,第二个是该函数的结果,该函数也恰好是函数

2.1 Function 示例

Function 源码

public interface Function<T, R> {
    R apply(T t);
}

示例

@Test
public void functions() {
    Function<String, Function<String, String>> functions = new Function<String, Function<String, String>>() {
        @Override
        public Function<String, String> apply(String str) {
            return new Function<String, String>() {
                @Override
                public String apply(String str2) {
                    return str + ";" + str2;
                }
            };
        }
    };

    Function<String, Function<String, String>> functions2 = new Function<String, Function<String, String>>() {
        @Override
        public Function<String, String> apply(String str) {
            return str2 -> str + ";" + str2;
        }
    };

    Function<String, Function<String, String>> functions3 = str -> str2 -> str + ";" + str2;

}

调用
String result = functions.apply("111").apply("222");

2.2 IntFunction 示例

IntFunction 与 IntUnaryOperator 的源码:

public interface IntFunction<R> {
    R apply(int value);
}

public interface IntUnaryOperator {
    int applyAsInt(int operand);
}

示例:

@Test
public void arrows() {

    IntFunction<IntUnaryOperator> curriedAdd = new IntFunction<IntUnaryOperator>() {
        @Override
        public IntUnaryOperator apply(int value) {
            return new IntUnaryOperator() {
                @Override
                public int applyAsInt(int operand) {
                    return operand + value;
                }
            };
        }
    };

    IntFunction<IntUnaryOperator> curriedAdd2 = new IntFunction<IntUnaryOperator>() {
        @Override
        public IntUnaryOperator apply(int value) {
            return operand -> operand + value;
        }
    };

    IntFunction<IntUnaryOperator> curriedAdd3 = value -> {
        return operand -> operand + value;
    };

    IntFunction<IntUnaryOperator> curriedAdd4 = value -> operand -> operand + value;
}

调用:

IntUnaryOperator apply = curriedAdd.apply(2);
int result = apply.applyAsInt(3);
System.out.println(result);

// 简化
int result2 = curriedAdd.apply(2).applyAsInt(3);

另一种方式

@Test
public void arrowsTwo() {
    IntFunction<IntUnaryOperator> curriedAdd = new IntFunction<IntUnaryOperator>() {
        @Override
        public IntUnaryOperator apply(int value) {
            return new IntUnaryOperator() {
                @Override
                public int applyAsInt(int operand) {
                    return operand + value;
                }
            };
        }
    };
    
    IntFunction<IntUnaryOperator> curriedAdd2 = value -> {
        return new IntUnaryOperator() {
            @Override
            public int applyAsInt(int operand) {
                return operand + value;
            }
        };
    };
    
    IntFunction<IntUnaryOperator> curriedAdd3 = value -> {
        return operand -> {
            return operand + value;
        };
    };
}

相关文章

网友评论

      本文标题:Java Lambda 表示式(箭头函数)

      本文链接:https://www.haomeiwen.com/subject/zecridtx.html