Lambda,具有多个参数的Java函数方法

mgdq6dx1  于 2022-10-22  发布在  Java
关注(0)|答案(1)|浏览(218)

我找到了指向TypeScript和Functional/Lammbda/Arrow方法的C#示例的链接。

TypeScript中的函数示例或TypeScript的箭头函数

https://blogs.halodoc.io/functional-typescript/

const square = (num: number): number => num*num;

https://www.tutorialsteacher.com/typescript/arrow-function

let sum = (x: number, y: number) => x + y;

C中的Lambda表达式#

i1 j2 k1 l

Func<int, int, int> add = (x, y) => x + y;

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions

Func<int, int, bool> testForEquality = (x, y) => x == y;

Java版本

https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Get-the-most-from-Java-Function-interface-with-this-example

Function<Integer, String> verboseLambda = (Integer x) -> { return Integer.toString(x*x); };
System.out.println(verboseLambda.apply(5));

但是,如何做这样的事情?

Function<Integer, Integer, String> squareSum = (x, y) -> Integer.toString(x*x + y*y);
vm0i2vca

vm0i2vca1#

但是,如何做这样的事情?:

Function<Integer, Integer, String> squareSum = (x, y) -> 
    Integer.toString(x * x + y * y);

为了提供两个参数,您需要一个BiFunction,而不是Function
表示接受两个参数并生成结果的函数。这是函数的二元特化。

BiFunction<Integer, Integer, String> squareSum = (x, y) -> 
    Integer.toString(x * x + y * y);

相关问题