Cur*_*len 11
事实证明,您可以使用asgroovy 1.x中的关键字将groovy Closure转换为java 8 lambda.
考虑以下java类
public class Foo {
public static boolean getBar(Predicate<String> predicate) {
return predicate.test("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
从Spock测试(在groovy中)我们可以写
class FooSpecTest extends Specification {
def "test foo"() {
when:
boolean result = Foo.getBar({x -> x.contains("blah")} as Predicate<String>)
then:
result == true
}
}
Run Code Online (Sandbox Code Playgroud)
根据您发布的链接,已在这些评论中回答.您可以尝试使用Closures.
根据官方Groovy文档:
Groovy 2.3不支持Java 8提供的新语法结构(例如lambdas,方法引用,接口中的默认方法等),但您很可能已经使用JDK 8提供的新API,甚至使用Groovy闭包代替Java 8 lambdas.
您使用的Groovy版本较旧,如果2.3不支持,您的版本则不会.
资料来源:http://www.groovy-lang.org/releasenotes/groovy-2.3.html
2.4中没有提到(http://www.groovy-lang.org/releasenotes/groovy-2.4.html)