1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
@Test
public void testFilterMapReduceMoyenne2() {
float average = Iterables2.reduce2(
FluentIterable.from(chiens).filter(new Predicate<Chien>() {
@Override
public boolean apply(Chien t) {
// filter que les males
return t.getSexe() == SEXE.MALE;
}
}).transform(new Function<Chien, Float>() {
@Override
public Float apply(Chien f) {
return Integer.valueOf(f.getAge()).floatValue();
}
}), new Function<List<Float>, Float>() {
@Override
public Float apply(List<Float> f) {
float sum = 0.0f;
if (f.isEmpty())
return 0.0f;
for(Float value : f) {
sum += value.floatValue();
}
return sum/f.size();
}
});
} |
Partager