Google – Abusing guava function predicate thingys

 

Instead of a loop, use the FluentIterable class to apply (Function) a change based on some condition (Predicate).

 

Predicate<Thingy> condition = new Predicate<Thingy thingy>() {


@Override


public
boolean apply(Thingy thingy) {


return
thingy.isItAllowed();

}

};

Function<Thingy, Thingy> apply = new Function<Thingy, Thingy>() {


@Override


public Thingy apply(Thingy thingy) {


// do something to your thingy


return
thingy;

}

};

FluentIterable.from(thingy.getThingys())

.filter(condition)

.transform(apply);

Simpler to loop over the list.

References.

https://google.github.io/guava/releases/19.0/api/docs/com/google/common/collect/FluentIterable.html