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 Stream
s 的正确方法是什么?
What is the proper way of converting it to use Java 8 Stream
s?
当我尝试使用 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 - 如何使用具有参数函数的谓词?
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01