Java 8 Lambdas和Streams ...不是“如何” ,但是“为什么”? [英] Java 8 Lambdas and Streams... not a "how" , but a "why"?

查看:147
本文介绍了Java 8 Lambdas和Streams ...不是“如何” ,但是“为什么”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题涉及Lambdas和Streams。有几件事我无法解决。以lambdas开头,以Predicate为例。

The question I have involves both Lambdas and Streams. There are a couple of things that I can't resolve. Starting with the lambdas, using Predicate as the example.

请注意以下代码中我如何导入 java.util.function.Predicate 并且我也没有实现类声明中的Predicate接口。然而,Lambda工作得很好。为什么?

Notice how in the following code I neither import "java.util.function.Predicate" nor do I implement the Predicate interface in the class declaration. And yet, the Lambda works just fine. Why is that?

public class Using_Predicate {
    public static List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

    public static void main(String[] args) {
        // passing the numbers List and different Lambdas to the intermediate
        // function.
        System.out.println();
        printVals(numbers, x -> x > 6); // all values greater than 6
        System.out.println();
        printVals(numbers, x -> x % 2 == 0); // all even values
        System.out.println();
        printVals(numbers, x -> x < 8); // ll values less than 8
        System.out.println();
        printVals(numbers, x -> x % 2 == 1); // all odd values
    }

    //intermediate Predicate function
    public static void printVals(List<Integer> val, Predicate<Integer>
            condition) {
        for (Integer v : val) {
            if (condition.test(v)) // if true, print v
                System.out.print(v + " ");
        }
    }
}

请注意我必须如何雇用中间函数,它利用Predicate函数接口的test()方法。
但是,如果我决定在使用流时做类似的事情,我再次无需导入java.util.function.Predicate或java.util.Stream,或在类声明中实现Predicate接口。此外,我可以在流中使用Predicate Lambda,甚至无需创建中间函数!为什么?

Notice how I have to employ an "intermediate function" that utilizes the "test()" method of the Predicate functional interface. However, if I decide to do something similar in using a stream, I again neither have to import java.util.function.Predicate, or java.util.Stream, or implement the Predicate interface in the class declaration. Furthermore, I can use a Predicate Lambda in the stream without even having to create an intermediate function! Why is that?

例如:

// a predicate lambda that prints the first value greater than 3, in this case 5
public class Sample1 {
    public static void main(String[] args) {
        List<Integer> values = Arrays.asList(1, 2, 3, 5, 4, 6, 7, 8, 9, 10);

        System.out.println(
                values.stream()
                        .filter(e -> e > 3)
                        .findFirst()
        );
    }
}

所以,我对为什么感到困惑Lambdas和溪流的规则,但不是如何。

So, I am really confused on the "why" of the rules for Lambdas and streams, but not so much on the "how".

推荐答案

进口主要用于允许你不必编写类的完整包名。因此,在这种情况下,如果在代码中的某处使用单词 Predicate ,则需要导入 Predicate 。例如,你必须使用单词 Predicate ,而是写出完整的包名, java.util.function.Predicate ,那么您不需要包含导入。

Imports are used mainly to allow you to not have to write the complete package name of a class. So in this case you would need to import Predicate if you use the word Predicate somewhere in your code. If for example you did have to use the word Predicate and instead wrote out the full package name, java.util.function.Predicate, then you would not need to include the import.

导入不用于为您的代码提供该类的公共API。因此,您无需导入谓词来为谓词创建一个lambda。编译器已经知道过滤器方法引用了哪个 Predicate 类。

Import is not used for providing your code with the public API of that class. So you do not need to import Predicate to create a lambda for Predicate. The compiler already knows which Predicate class is being referred by the filter method.

这篇关于Java 8 Lambdas和Streams ...不是“如何” ,但是“为什么”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆