简体   繁体   English

使用 Java 谓词和 Lambda

[英]Using Java Predicate and Lambda

Why does the below code return Predicate<String> and not boolean ?为什么下面的代码返回Predicate<String>而不是boolean

My understanding is that the !s.isEmpty() check here is going against the Predicate boolean test(T t);我的理解是这里的!s.isEmpty()检查与 Predicate boolean test(T t); The return type here is boolean .这里的返回类型是boolean

So in my lambda should my nonEmptyStringPredicate not be of type boolean ?所以在我的 lambda 中,我的nonEmptyStringPredicate不应该是boolean类型吗? Obviously, it's not, I'm just trying to understand why it's not.显然,它不是,我只是想了解为什么不是。

Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();

A Predicate gets in this case a String as parameter and returns a boolean .在这种情况下, Predicate获取String作为参数并返回boolean In case we don't write it as lambda it would look like this:如果我们不把它写成 lambda,它看起来像这样:

Predicate<String> somePredicate = new Predicate<String>() {
    @Override
    public boolean test(String string) {
        return !string.isEmpty();
    }
};

If you really are willing to get a boolean though from the Predicate , you can use its test method:如果你真的愿意从Predicate得到一个布尔值,你可以使用它的test方法:

Predicate<String> nonEmptyStringPredicate = s -> !s.isEmpty();
boolean val = nonEmptyStringPredicate.test("any"); // true

Predicate on the other hand is just a FunctionalInterface , that you've represented using a lambda expression.另一方面, Predicate只是一个FunctionalInterface ,您使用 lambda 表达式表示。

Lambda is like an anonymous class, so this: Lambda 就像一个匿名类,所以这个:

Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();

Is like:就好像:

Predicate<String> nonEmptyStringPredicate = new Predicate<String>() {
    public boolean test(String s) {
        return !s.isEmpty();
    }
}

Why does the below code return Predicate and not boolean?为什么下面的代码返回 Predicate 而不是布尔值?

This is because the type of the function (String s) -> !s.isEmpty() is a Predicate<String> , at this point you're simply defining a function (which says "given a String as input return a boolean value indication whether it's empty or not).这是因为函数的类型(String s) -> !s.isEmpty()Predicate<String> ,此时您只是定义了一个函数(它说“给定一个字符串作为输入返回一个布尔值指示它是否为空)。

Note that at this point you're not evaluating anything hence the result is not a boolean but rather a function .请注意,此时您没有评估任何内容,因此结果不是 boolean 而是function

Definition of FI from the doc:文档中 FI 的定义:

Functional interfaces provide target types for lambda expressions and method references.函数式接口为 lambda 表达式和方法引用提供目标类型。 Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression's parameter and return types are matched or adapted.每个函数式接口都有一个抽象方法,称为该函数式接口的函数式方法,lambda 表达式的参数和返回类型与之匹配或适应。 Functional interfaces can provide a target type in multiple contexts, such as assignment context, method invocation, or cast context:函数式接口可以在多个上下文中提供目标类型,例如赋值上下文、方法调用或强制转换上下文:

in order to get the "boolean result" you're seeking, you must first invoke the "functional method".为了获得您正在寻求的“布尔结果”,您必须首先调用“功能方法”。 example:例子:

Predicate<String> nonEmptyStringPredicate = s -> !s.isEmpty();
boolean result = nonEmptyStringPredicate.test(""); 

A predicate always returns a boolean so the only interesting value here is the input parameter which is a String.谓词总是返回一个布尔值,所以这里唯一有趣的值是输入参数,它是一个字符串。

See: https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html请参阅: https : //docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

It returns a predicate because on the left hand side you declared predicate as return type, and on the right side you are returning the implementation of Predicate's test() method.它返回一个谓词,因为在左侧,您将谓词声明为返回类型,而在右侧,您将返回 Predicate 的test()方法的实现。 When your code returns an object which is the inplementation of a functional interface (like in your case) you simply return the logic of its single abstract method, in this case test() So basically is as ifyou are returning this: Predicate<String> nonEmptyStringPredicate = new Predicate { @Override boolean test(String input) { return input.isEmpty() } } When you write the code as you wrote it, the compiler 'knows' that !s.isEmpty() is the logic of method test , it basically tells you "hey give me just the logic, i dont need the boilerplate code" Btw is good practice to use method reference, you can replace !s.isEmpty() with !String::isEmpty当您的代码返回一个作为功能接口实现的对象时(如您的情况),您只需返回其单个抽象方法的逻辑,在本例中为test()所以基本上就像您正在返回: Predicate<String> nonEmptyStringPredicate = new Predicate { @Override boolean test(String input) { return input.isEmpty() } }当你写代码时,编译器“知道” !s.isEmpty()是方法test的逻辑,它基本上告诉你“嘿给我逻辑,我不需要样板代码”顺便说一句是使用方法引用的好习惯,你可以用!String::isEmpty替换!s.isEmpty()

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

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