Java 8 - How to use predicate that has a function with parameter?(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 - 如何使用具有参数函数的谓词?
基础教程推荐
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
