Java 8 - 如何使用具有参数函数的谓词?

Java 8 - How to use predicate that has a function with parameter?(Java 8 - 如何使用具有参数函数的谓词?)

本文介绍了Java 8 - 如何使用具有参数函数的谓词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public boolean isImageSrcExists(String imageSrc) {
    int resultsNum = 0;
    List<WebElement> blogImagesList = driver.findElements(blogImageLocator);

    for (WebElement thisImage : blogImagesList) {
        if (thisImage.getAttribute("style").contains(imageSrc)) {
            resultsNum++;
        }
    }

    if (resultsNum == 2) {
        return true;
    } else {
        return false;
    }
}

将其转换为使用 Java 8 Streams 的正确方法是什么?

What is the proper way of converting it to use Java 8 Streams?

当我尝试使用 map() 时,我收到一个错误,因为 getAttribute 不是 Function.

When I'm trying to use map(), I get an error since getAttribute isn't a Function.

int a = (int) blogImagesList.stream()
                            .map(WebElement::getAttribute("style"))
                            .filter(s -> s.contains(imageSrc))
                            .count();

推荐答案

你不能完全按照你的意愿去做 - 方法引用中不允许显式参数.

You cannot do exactly what you want - explicit parameters are not allowed in method references.

但你可以...

...创建一个返回布尔值的方法,并对 getAttribute("style") 的调用进行编码:

...create a method that returns a boolean and harcoded the call to getAttribute("style"):

public boolean getAttribute(final T t) {
    return t.getAttribute("style");
}

这将允许您使用方法 ref:

This would allow you to use the method ref:

int a = (int) blogImagesList.stream()
              .map(this::getAttribute)
              .filter(s -> s.contains(imageSrc))
              .count();

...或者你可以定义一个变量来保存函数:

...or you could define a variable to hold the function:

final Function<T, R> mapper = t -> t.getAttribute("style");

这将允许您简单地传递变量

This would allow you to simply pass the variable

int a = (int) blogImagesList.stream()
              .map(mapper)
              .filter(s -> s.contains(imageSrc))
              .count();

...或者您可以将上述两种方法结合起来(这肯定是非常过分的)

...or you could curry and combine the above two approaches (this is certainly horribly overkill)

public Function<T,R> toAttributeExtractor(String attrName) {
    return t -> t.getAttribute(attrName);
}

然后您需要调用 toAttributeExtractor 来获取 Function 并将其传递到 map:

Then you would need to call toAttributeExtractor to get a Function and pass that into the map:

final Function<T, R> mapper = toAttributeExtractor("style");
int a = (int) blogImagesList.stream()
              .map(mapper)
              .filter(s -> s.contains(imageSrc))
              .count();

尽管实际上,简单地使用 lambda 会更容易(就像您在下一行中所做的那样):

Although, realistically, simply using a lambda would be easier (as you do on the next line):

int a = (int) blogImagesList.stream()
              .map(t -> t.getAttribute("style"))
              .filter(s -> s.contains(imageSrc))
              .count();

这篇关于Java 8 - 如何使用具有参数函数的谓词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 8 - 如何使用具有参数函数的谓词?

基础教程推荐