英文:
How the Lambda in static method instantiates the interface
问题
我已经在做Java开发有一段时间了,由于公司和个人的惯性,大部分时间都在使用Java 1.7版本。最近,我决定开始学习更多关于Java函数式编程方面的内容,所以我开始尝试使用Function接口,并构建一个**验证器(Validator)**,用于检查**一个Map是否符合验证器的条件**。
这样,验证器可以按照以下方式进行程序化构建:
使用示例:
```java
Map<String, String> book = new LinkedHashMap<String, String>();
book.put("Title", "Leviathan Wakes");
book.put("Subject", "A proto molecule");
book.put("Category", "sci-fi");
book.put("TotalPages", "350");
final ResourceValidator filter = ResourceValidator.containsProperty("Title").and(ResourceValidator.containsProperty("Category"));
assert filter.apply(book);
这是我编写的代码:
public interface ResourceValidator extends Function<Map<String, String>, Boolean> {
static ResourceValidator containsProperty(String property) {
return resource -> resource.containsKey(property);
}
default ResourceValidator and(ResourceValidator other) {
return resource -> this.apply(resource) && other.apply(resource);
}
}
到目前为止,它似乎表现得如预期那样。我不能理解的是接口是如何被实例化的。对于以下这个调用:
final ResourceValidator filter = ResourceValidator.containsProperty("Title");
调用了接口的静态方法,在静态方法中,ResourceValidator 是如何被实例化的呢?
static ResourceValidator containsProperty(String property) {
return resource -> resource.containsKey(property);
}
我一直在寻找答案,但我一直在努力中。
<details>
<summary>英文:</summary>
I've been doing java for some time, due to corporate and personal inertia, mostly java 1.7.
Recently I decided to start learning a bit more about the functional side o java, so I started playing with the Function interface and building a **Validator** that would check **if a Map would match the Validator criteria**.
So that the Validator could be built programatically like
Usage:
Map<String, String> book = new LinkedHashMap<String, String>();
book.put("Title", "Leviathan Wakes");
book.put("Subject", "A proto molecule");
book.put("Category", "sci-fi");
book.put("TotalPages", "350")
final ResourceValidator filter = ResourceValidator.containsProperty("Title").and(ResourceValidator.containsProperty("Category"));
assert filter.apply(book);
This is code I've produced
public interface ResourceValidator extends Function<Map<String,String>,Boolean> {
static ResourceValidator containsProperty(String property) {
return resource -> resource.containsKey(property);
}
default ResourceValidator and(ResourceValidator other){
return resource -> this.apply(resource) && other.apply(resource);
}
}
So far it seems to be behaving as expected, what I can't understand is how the interface gets instantiated.
The call to
final ResourceValidator filter = ResourceValidator.containsProperty("Title");
calls static method of the interface, **how and where in the static method is ResourceValidator instantiated?**
static ResourceValidator containsProperty(String property) {
return resource -> resource.containsKey(property);
}
I've been looking and I'm struggling to find an answer
</details>
# 答案1
**得分**: 0
你提供了这些静态方法的实现。例如,当你创建一个类似 `resource -> resource.containsKey(property)` 的 lambda 表达式时,实际上你所做的是:
```java
return new ResourceValidator() {
@Override
public Boolean apply(Map<String, String> resource) {
return resource.containsKey(property);
}
};
你实现了 ResourceValidator
函数的 apply
方法(它是 Function
接口的子类型,被标记为函数式接口)。
现在你可以像这样调用它:
ResourceValidator.containsProperty("foo").apply(Map.of("foo", "bar"));
你可能已经遇到过 Java lambda 表达式。如果没有,这是一个相当全面的资源:https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
英文:
You provided the implementation of those static methods. For example; when you created a lambda as resource -> resource.containsKey(property)
, what you essentially did was;
return new ResourceValidator() {
@Override
public Boolean apply(Map<String, String> resource) {
return resource.containsKey(property);
}
};
you implemented the apply
of ResourceValidator
function (is subtype of Function
interface, which is marked as a functional interface).
So now you could invoke it like;
ResourceValidator.containsProperty("foo").apply(Map.of("foo", "bar"));
You may have come across Java lambda expressions. Otherwise, this is quite comprehensive https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论