从 Java 8 中的方法返回 Lambda?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26771953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:15:04  来源:igfitidea点击:

Return Lambda from Method in Java 8?

javalambdajava-8

提问by Philip Lombardi

I have just begun to use Java 8 and I am wondering if there is a way to write a method that returns a Function?

我刚刚开始使用 Java 8,我想知道是否有一种方法可以编写一个返回Function?

Right now I have method like below:

现在我有如下方法:

Function<Integer, String> getMyFunction() {
  return new Function<Integer, String>() {
    @Override public String apply(Integer integer) {
      return "Hello, world!"
    }
  } 
}

Is there a way to write that more succinctly in Java 8? I was hoping this would work but it does not:

有没有办法在 Java 8 中更简洁地编写它?我希望这会起作用,但它不起作用:

Function<Integer, String> getMyFunction() {
  return (it) -> { return "Hello, world: " + it } 
}

采纳答案by mkobit

Get rid of your return statement inside of your function definition:

去掉函数定义中的 return 语句:

Function<Integer, String> getMyFunction() {
    return (it) -> "Hello, world: " + it;
}

回答by assylias

You are missing semi colons:

您缺少分号:

return (it) -> { return "Hello, world: " + it; };

Although as noted it can be shortened to:

虽然如前所述,它可以缩短为:

return it -> "Hello, world: " + it;

回答by Oussama Zoghlami

You could write it simply like that:

你可以简单地这样写:

Function<Integer, String> function = n -> "Hello, world " + n;

回答by Jo?o Antunes

So, the answer for 99% of the cases has been given by @assylias

所以,@assylias 已经给出了 99% 的答案

You are missing semi colons:

return (it) -> { return "Hello, world: " + it; }; Although as noted itcan be shortened to:

return it -> "Hello, world: " + it;

您缺少分号:

return (it) -> { return "Hello, world: " + it; }; Although as noted it可以缩短为:

return it -> "Hello, world: " + it;

Yet, I think that it's worth it to add that, if you want to assign your lambda to a variable (to use later). You can do so by typing:

但是,如果您想将 lambda 分配给变量(稍后使用),我认为值得添加这一点。您可以通过键入:

Callable<YourClass> findIt = () -> returnInstanceOfYourClass();

And then you can easily use it, one example of such a use:

然后你就可以很容易地使用它了,一个这样使用的例子:

if(dontNeedzToWrap()) {
                return findIt.call();
  }
return Wrapp.withTransaction(() -> findIt.call());

Given, things can be even made simpler if the Wrapp.withTransaction()method accepts the same kind of Callable's as parameters. (I use this for JPA atm)

鉴于,如果该Wrapp.withTransaction()方法接受相同类型的 Callable 作为参数,事情甚至可以变得更简单。(我将此用于 JPA atm)

回答by Magnilex

I would like to point out that it might be more appropriate to use the built-in IntFunctionin this case:

我想指出,IntFunction在这种情况下使用内置函数可能更合适:

IntFunction<String> getMyFunction() {
    return it -> "Hello, world: " + it;
}

IntFunctionis a part of the standard API for functional interfaceswhich defines a range of good to have interfaces, mostly related to Java primitives.

IntFunction函数式接口标准 API的一部分,它定义了一系列良好的接口,主要与 Java 原语相关。