我正在玩Java 8,以了解如何作为一等公民的功能。我有以下代码段:
package test;
import java.util.*;
import java.util.function.*;
public class Test {
public static void myForEach(List<Integer> list, Function<Integer, Void> myFunction) {
list.forEach(functionToBlock(myFunction));
}
public static void displayInt(Integer i) {
System.out.println(i);
}
public static void main(String[] args) {
List<Integer> theList = new ArrayList<>();
theList.add(1);
theList.add(2);
theList.add(3);
theList.add(4);
theList.add(5);
theList.add(6);
myForEach(theList, Test::displayInt);
}
}
我要做的是使用方法引用将方法displayInt
传递给方法myForEach
。编译器会产生以下错误:
src/test/Test.java:9: error: cannot find symbol
list.forEach(functionToBlock(myFunction));
^
symbol: method functionToBlock(Function<Integer,Void>)
location: class Test
src/test/Test.java:25: error: method myForEach in class Test cannot be applied to given ty
pes;
myForEach(theList, Test::displayInt);
^
required: List<Integer>,Function<Integer,Void>
found: List<Integer>,Test::displayInt
reason: argument mismatch; bad return type in method reference
void cannot be converted to Void
编译器抱怨void cannot be converted to Void
。我不知道如何在myForEach
的签名中指定函数接口的类型,以便代码编译。我知道我只需将displayInt
的返回类型更改为Void
,然后返回null
。但是,在某些情况下,我无法改变我想要传递到其他地方的方法。是否有一种简单的方法可以重复使用displayInt
?
答案 0 :(得分:192)
您正在尝试使用错误的接口类型。在这种情况下,类型Function不合适,因为它接收参数并具有返回值。相反,您应该使用Consumer(以前称为Block)
函数类型声明为
interface Function<T,R> {
R apply(T t);
}
但是,消费者类型与您正在寻找的类型兼容:
interface Consumer<T> {
void accept(T t);
}
因此,Consumer与接收T的方法兼容并且不返回任何内容(void)。这就是你想要的。
例如,如果我想在列表中显示所有元素,我可以使用lambda表达式为其创建一个使用者:
List<String> allJedi = asList("Luke","Obiwan","Quigon");
allJedi.forEach( jedi -> System.out.println(jedi) );
您可以在上面看到,在这种情况下,lambda表达式接收一个参数并且没有返回值。
现在,如果我想使用方法引用而不是lambda表达式来创建此类型的消耗,那么我需要一个接收String并返回void的方法,对吧?
我可以使用不同类型的方法引用,但在这种情况下,让我们使用println
对象中的System.out
方法来利用对象方法引用,如下所示:
Consumer<String> block = System.out::println
或者我可以简单地做
allJedi.forEach(System.out::println);
println
方法是合适的,因为它接收一个值并且返回类型为void,就像Consumer中的accept
方法一样。
因此,在您的代码中,您需要将方法签名更改为:
public static void myForEach(List<Integer> list, Consumer<Integer> myBlock) {
list.forEach(myBlock);
}
然后你应该能够在你的情况下使用静态方法引用创建一个使用者:
myForEach(theList, Test::displayInt);
最终,你甚至可以完全摆脱你的myForEach
方法而只是这样做:
theList.forEach(Test::displayInt);
关于作为头等公民的职能
所有人都说过,事实是Java 8将不具备作为一等公民的功能,因为结构功能类型不会被添加到语言中。 Java将简单地提供一种从lambda表达式和方法引用中创建功能接口实现的替代方法。最终lambda表达式和方法引用将绑定到对象引用,因此我们所有的对象都是一等公民。重要的是功能就在那里因为我们可以将对象作为参数传递,将它们绑定到变量引用并将它们作为其他方法的值返回,然后它们几乎起到了类似的作用。
答案 1 :(得分:15)
当你需要接受一个函数作为参数而不接受任何参数并且没有返回结果(void)时,在我看来仍然最好有类似的东西
private interface Thunk { void apply(); }
代码中的某处。在我的函数式编程课程中,单词“thunk&#39;用于描述这些功能。为什么它不在java.util.function中是我无法理解的。
在其他情况下,我发现即使java.util.function确实具有与我想要的签名相匹配的东西 - 当界面的命名与使用不匹配时,它仍然总是感觉不对我的代码中的函数。我想这与其他地方关于&#39; Runnable&#39; - 这是一个与Thread类相关的术语 - 所以虽然它可能有我需要的签名,但它仍然可能使读者感到困惑。
答案 2 :(得分:0)
我觉得您应该使用使用者界面而不是Function<T, R>
。
使用者基本上是一个功能性接口,旨在接受一个值并且不返回任何值(即无效)
在您的情况下,您可以在代码的其他位置创建使用者,如下所示:
Consumer<Integer> myFunction = x -> {
System.out.println("processing value: " + x);
.... do some more things with "x" which returns nothing...
}
然后,您可以将您的myForEach
代码替换为以下代码段:
public static void myForEach(List<Integer> list, Consumer<Integer> myFunction)
{
list.forEach(x->myFunction.accept(x));
}
您将myFunction视为一流的对象。
答案 3 :(得分:0)
将返回类型设置为Void
,而不是void
和return null
// Modify existing method
public static Void displayInt(Integer i) {
System.out.println(i);
return null;
}
OR
// Or use Lambda
myForEach(theList, i -> {System.out.println(i);return null;});