Lambda表达式在静态方法中如何实例化接口。

huangapple go评论77阅读模式
英文:

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&#39;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&lt;String, String&gt; book = new LinkedHashMap&lt;String, String&gt;();
    
    book.put(&quot;Title&quot;, &quot;Leviathan Wakes&quot;);
    book.put(&quot;Subject&quot;, &quot;A proto molecule&quot;);
    book.put(&quot;Category&quot;, &quot;sci-fi&quot;);
    book.put(&quot;TotalPages&quot;, &quot;350&quot;)
    
    final ResourceValidator filter = ResourceValidator.containsProperty(&quot;Title&quot;).and(ResourceValidator.containsProperty(&quot;Category&quot;));
    assert filter.apply(book);


This is code I&#39;ve produced  


        public interface ResourceValidator extends Function&lt;Map&lt;String,String&gt;,Boolean&gt; {
        
        static ResourceValidator containsProperty(String property) {
                return resource -&gt; resource.containsKey(property);
            }
    
        default ResourceValidator and(ResourceValidator other){
            return resource -&gt; this.apply(resource) &amp;&amp; other.apply(resource);
        }
    }


So far it seems to be behaving as expected, what I can&#39;t understand is how  the interface gets instantiated.
The call to 

    final ResourceValidator filter = ResourceValidator.containsProperty(&quot;Title&quot;);

calls static method of the interface, **how and where in the static method is ResourceValidator  instantiated?**

    static ResourceValidator containsProperty(String property) {
                return resource -&gt; resource.containsKey(property);
            }


I&#39;ve been looking and I&#39;m struggling to find an answer 



</details>


# 答案1
**得分**: 0

你提供了这些静态方法的实现。例如,当你创建一个类似 `resource -&gt; resource.containsKey(property)` 的 lambda 表达式时,实际上你所做的是:

```java
return new ResourceValidator() {
    @Override
    public Boolean apply(Map&lt;String, String&gt; resource) {
        return resource.containsKey(property);
    }
};

你实现了 ResourceValidator 函数的 apply 方法(它是 Function 接口的子类型,被标记为函数式接口)。

现在你可以像这样调用它:

ResourceValidator.containsProperty(&quot;foo&quot;).apply(Map.of(&quot;foo&quot;, &quot;bar&quot;));

你可能已经遇到过 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 -&gt; resource.containsKey(property), what you essentially did was;

 return new ResourceValidator() {
            @Override
            public Boolean apply(Map&lt;String, String&gt; 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(&quot;foo&quot;).apply(Map.of(&quot;foo&quot;, &quot;bar&quot;));

You may have come across Java lambda expressions. Otherwise, this is quite comprehensive https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html,

huangapple
  • 本文由 发表于 2020年10月11日 06:05:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64298743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定