The answer of @Surodip uses a compact solution based on Apache Commons Collections.
But that solution is not typesafe, since the Transfomer references the property via string expression: TransformerUtils.invokerTransformer("getName")
Here is a more verbose, but typesafe solution using Apache Commons Collections:
Collection<String> values = CollectionUtils.collect(messages, new Transformer<Obj, String>(){
@Override
public String transform(Obj input) {
return input.getFoo();
}
});
The above solutions uses Apache Commons Collection Version >= 4, which supports generics for type safety.
Below is the less typesafe version for Apache Collections Version < 4, which does not use generics:
Collection values = CollectionUtils.collect(messages, new Transformer(){
@Override
public Object transform(Object input) {
Obj obj = (Obj) input;
return obj.getFoo();
}
});