简体   繁体   English

lambda 表达式如何初始化参数?

[英]How lambda expression initialize parameter?

I'm confused with lamda expression.我对 lamda 表达式感到困惑。

JavaDStream<ConsumerRecord<String, String>> rsvpsWithGuestsStream =
        meetupStream.filter(f -> !f.value().contains("\"guests\":0"));

rsvpsWithGuestsStream.foreachRDD((JavaRDD<ConsumerRecord<String, String>> r) -> {        
    MongoSpark.save(
            r.map(
                e -> Document.parse(e.value())
            )
    );            
});

Here is a foreachRDD method void foreachRDD(VoidFunction<R> foreachFunc) , It accepts a functional interface.这是一个 foreachRDD 方法void foreachRDD(VoidFunction<R> foreachFunc) ,它接受一个函数式接口。

And in code, JavaRDD<ConsumerRecord<String, String>> r passed as a argument which is internally used by its call method.在代码中, JavaRDD<ConsumerRecord<String, String>> r作为参数传递,该参数在其call方法内部使用。

I want to know Does lambda expression initialize r on its own ?我想知道 lambda 表达式是否自己初始化r Becasue it can call map only if its initialized.因为它只有在初始化后才能调用map And In code I cant see anywhere its already created.在代码中,我看不到它已经创建的任何地方。

Can anyone help me to understand this ?谁能帮我理解这一点?

A lambda expression does not initialize anything.一个 lambda 表达式不初始化任何东西。 It is just an anonymous function which does something with its arguments - assuming they have been already initialized.它只是一个匿名函数,它用它的参数做一些事情——假设它们已经被初始化。 I am not familiar with your use case (guess it is Spark) but it looks like r is just one of the elements in the stream, and each such element is passed in to your lambda.我不熟悉您的用例(猜测它是 Spark),但看起来 r 只是流中的元素之一,并且每个这样的元素都传递给您的 lambda。 Below is a similar but much simpler example:下面是一个类似但更简单的例子:

Stream.of("a", "b", "c").forEach(el -> System.out.println(el));

Here el is each of the elements "a", "b" and "c", passed in to the lambda which simply prints it.这里 el 是元素“a”、“b”和“c”中的每一个,传递给 lambda ,它只是打印它。 Just like a function, a lambda doesn't know anything about its arguments and whether they are initialiazed - this is up to the caller, in the above cases - the forEach methods.就像一个函数一样,一个 lambda 不知道它的参数以及它们是否被初始化——这取决于调用者,在上述情况下——forEach 方法。

Here is a quote from Java spec:这是 Java 规范的引述:

15.27.1. 15.27.1. Lambda Parameters Lambda 参数

When the lambda expression is invoked (via a method invocation expression (§15.12)), the values of the actual argument expressions initialize newly created parameter variables, each of the declared or inferred type, before execution of the lambda body.当调用 lambda 表达式时(通过方法调用表达式(第 15.12 节)),实际参数表达式的值在执行 lambda 主体之前初始化新创建的参数变量,每个声明或推断类型。 The Identifier that appears in the LambdaParameter or directly in the LambdaParameterList or LambdaParameters may be used as a simple name in the lambda body to refer to the formal parameter.出现在 LambdaParameter 中或直接出现在 LambdaParameterList 或 LambdaParameters 中的标识符可以用作 lambda 主体中的简单名称来引用形式参数。

https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html#jls-15.27.1 https://docs.oracle.com/javase/specs/jls/se15/html/jls-15.html#jls-15.27.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM