[英]Java stream API - avoid using same predicate twice to calculate average
class A {
double value;
String key;
}
class B {
List<A> aList;
}
Given List<B> bList
, I want to calculate the average of the value
s for a specific key k
. 给定
List<B> bList
,我想计算特定密钥k
的value
s的平均值。 Assume each B
can hold only 0 or 1 times an instance of A
with key.equals(k)
. 假设每个
B
只能用key.equals(k)
保持A
的实例的0或1倍。
I figured I could first filter aList
, and later extract the value using mapToDouble
: 我想我可以先过滤
aList
,然后使用mapToDouble
提取值:
double average = blist.stream().filter(
b -> b.aList.stream().anyMatch(
a -> a.key.equals(k)
)
).mapToDouble(
b -> b.aList.stream().filter(
a -> a.key.equals(k)
).findFirst().get().value
).average().orElse(0);
But there is clearly a redundancy here, since I am filtering the same list by the same predicate twice ( a -> a.key.equals(k)
). 但是这里显然有一个冗余,因为我用相同的谓词过滤相同的列表两次(
a -> a.key.equals(k)
)。
Is there a way to mapToDouble
while omitting elements with missing matching key
s at the same time? 有没有办法
mapToDouble
同时省略缺少匹配key
s的元素?
Edit: 编辑:
Here is a more concrete example, hopefully it will make it easier to understand: 这是一个更具体的例子,希望它更容易理解:
String courseName;
...
double average = students.stream().filter(
student -> student.getGrades().stream().anyMatch(
grade -> grade.getCourseName().equals(courseName)
)
).mapToDouble(
student -> student.getGrades().stream().filter(
grade -> grade.getCourseName().equals(courseName)
).findFirst().get().getValue()
).average().orElse(0);
System.out.println(courseName + " average: " + average);
Try this: 尝试这个:
double average = bList.stream()
.flatMap(b -> b.aList.stream())
.filter(a -> a.key.equals(k))
.mapToDouble(a -> a.value)
.average()
.orElse(Double.NaN);
If your objects have private field and getters, which they really should, it'd be like this: 如果你的对象有私有字段和getter,他们真的应该这样,它就像这样:
double average = bList.stream()
.map(B::getaList)
.flatMap(List::stream)
.filter(a -> a.getKey().equals(k))
.mapToDouble(A::getValue)
.average()
.orElse(Double.NaN);
Try this. 尝试这个。
double average = blist.stream()
.map(b -> b.aList.stream()
.filter(a -> a.key.equals(k))
.findFirst())
.filter(a -> a.isPresent())
.mapToDouble(a -> a.get().value)
.average().orElse(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.