用于引导lambda表达式的实用程序类或方法链接的方法引用? [英] Utility Class to bootstrap lambda expressions or method references for method chaining?

查看:119
本文介绍了用于引导lambda表达式的实用程序类或方法链接的方法引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Java 8中引入的功能接口,您可以轻松地将不同的表达式链接到一个新表达式中,如下面的代码片段所示。

With the functional interfaces introduced in Java 8, you can easily chain different expressions into one new expression, illustrated by the code snippet below.

public class PredicateChaining {
    public static void main(String[] args) {
        // verbose, but standard JDK functionality only
        Predicate<String> allUpperCase = StringUtils::isAllUpperCase;
        Predicate<String> longerThan5 = s -> s.length() > 5;
        if (allUpperCase.and(longerThan5).test("PREDICATE")) {
            System.out.println("'PREDICATE' is a uppercase text, longer than 5.");
        }

        // compact, but with utility method ('chain' in this case)
        if (chain(StringUtils::isAllLowerCase).and(s -> s.length() < 5).test("test")) {
            System.out.println("'test' is a lowercase text, shorter than 5.");
        }
    }

    public static <T> Predicate<T> chain(Predicate<T> test) {
        return test;
    }
}

在这种情况下2 String Predicate s被链接到一个新的 Predicate 中。当你有至少一个句柄(即变量)来调用链接方法(在本例中为'')时,这很好。但是,如果要链接的表达式表示为lambda或方法引用,则需要将其中一个显式转换为变量,这使得它非常详细。当编译器无法确定确切的类型时,您必须采用这种结构。但在大多数情况下,这只是我想避免的冗长开销。

In this case 2 String Predicates are chained into one new Predicate. This works fine when you have at least one handle (i.e. variable) to invoke the chaining method (in this case 'and') on. However, if the expressions you want to chain are represented as a lambda or method reference, you need to translate one of them explicitly to a variable, which makes it very verbose. When the compiler is unable to determine the exact type, you have to resort to this kind of construction. But in most cases, this is simply verbose overhead I'd like to avoid.

在这个例子中,我编写了实用方法' chain '我自己。
但我可以为其他功能界面创建一个(功能消费者供应商 BiConsumer ,...)以及。

In this example, I have written the utility method 'chain' myself. But I can create one for the other functional interfaces (Function, Consumer, Supplier, BiConsumer, ...) as well.

我没有使用这些方法创建自己的实用程序类(我可能会将其复制粘贴到我工作的每个项目中),想知道这样的类是存在于JDK还是某些库中?

Instead of creating my own utility class with these methods (which I would probably copy-paste to every project I work on), I want to know whether such a class exists in the JDK or in some library?

另一方面,如果这样的课程不存在而且我不应该自己创建,我想听听原因。

On the other hand, if such a class does not exist and I shouldn't be creating one myself, I would like to hear why.

推荐答案

在后续<$中创建 Predicate 实例是没有意义的c $ c> if statement。

There is no sense in creating Predicate instances to use them in a follow-up if statement.

Predicate 实例通常用于传递a一种方法的条件。在这种情况下,您可以研究特定的API,以确定是否可以使用自然结构进行链接。

Predicate instances are normally used for passing a condition to a method. In this case, you may study the particular API to find out whether such a chaining is possible using natural constructs.

例如。将谓词传递给流时,您可以简单地进行两次或更多过滤调用:

E.g. when passing Predicates to a stream you may simply make two or more filter calls:

stringStream.filter(StringUtils::isAllUpperCase).filter(s->s.length()>5). …

(您也可以链接功能 s使用多个 map 调用)

(and you may similarly chain Functions using multiple map calls)

和其他API可能有类似的方法。

and other APIs may have similar methods.

但毕竟,如果你有两个条件要组合,而且两个条件都不是谓词实例,直接的解决方案就是组合表达式:

But after all, if you have two conditions which you want to combine and neither is already a Predicate instance, the straight forward solution is to just combine the expressions:

stringStream.filter(s -> isAllUpperCase(s) && s.length()>5). …

或作为独立创作:

Predicate<String> allUpperCaseAndLongerThan5 = s -> isAllUpperCase(s) && s.length()>5;

请注意,您可以使用静态导入这里的实用方法允许更紧凑的表达式。

note that you can use a static import for the utility method here which allows an even more compact expression.

使用 Predicate.and Predicate.or 仅在您已经拥有 Predicate 实例且想要扩充其逻辑时才有用。但在这种情况下,您还可以使用一个实例来调用而不会出现问题。

Using Predicate.and and Predicate.or is only useful if you already have a Predicate instance and want to augment its logic. But in this case you also have an instance on which you can invoke and or or without problems.

这篇关于用于引导lambda表达式的实用程序类或方法链接的方法引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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