To be very simple I have
class Per{
int a;
long b;
double c;
String d;
}
Let say I have 3000 Object of Type Per
and collected in a List<Per> pers
Now I want to achieve:-
- Skip if object is null or
d
isnull
orblank
- sum of
a
- sum of
b
- aggregated value of operation performed on
c
Old way is
int totalA = 0; long totalB = 0l; long totalC = 0l;
for (Per per : pers) {
if (per.d != null && !per.d.trim().equals("")) {
totalA += per.a;
totalB += per.b;
totalC += someOperation(per.c);
}
}
someOperation
implemention is not important as it may be simple or complex.
How can I achieve this via Java8 streams and lambda expression?
A possible answer would be like this
int totalA = 0; long totalB=0l;
pers.stream().filter(p->{
if(p == null || p.d == null || p.d.trim().equals(""))
return false;
return true;
}).forEach(per->{
totalA += per.a;
totalB += per.b;
});
But totalA and totalB must be final or effectively final
Other possibilities in my mind are:
1 re-use the filtered stream using Supplier
as suggested then apply map and aggregation operations.
2 Collect the filtered Per
and then do forEach
NOTE I'm not doing any kind of grouping here and can't create new Per
or update provided objects.