0
我在使用lambda表達式的Java 8語法時遇到了問題,我似乎無法在一行中創建一個。下面的代碼工作,Java 8:將java.util.function.Function作爲Lambda表達式實現的語法
Function<Integer, Integer> increment = (x) -> (x + 1);
doStuff(increment);
但以下行沒有
doStuff((x) -> (x + 1));
doStuff(Function (x) -> (x + 1));
doStuff(new Function (x) -> (x + 1));
doStuff(Function<Integer, Integer> (x) -> (x + 1));
doStuff(new Function(Integer, Integer> (x) -> (x + 1));
doStuff(new Function<Integer, Integer>(x -> {x + 1;}));
,我不太清楚還有什麼我可以試試。我當然不想使用
doStuff(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer x){
return x + 1;
}
});
那麼還有什麼呢?我查看了一堆關於lambda表達式語法的問題,但似乎沒有任何工作。
也不管用,'doStuff'需要'Function'。 – user3002473 2014-12-07 00:25:57
@ user3002473請解釋你所得到的錯誤。請給出'doStuff'的簽名。 – 2014-12-07 00:27:10
當我離開時是'(x) - >(x + 1)',它給了我錯誤'二元運算符的錯誤操作數類型'+''。如果我把'(Integer x) - >(x + 1)',它表示沒有爲doStuff((int x) - >(x + 1))找到合適的方法。 – user3002473 2014-12-07 00:28:54