Guava: how to combine filter and transform?-open source projects google/guava
I have a collection of Strings, and I would like to convert it to a collection of strings were all empty or null Strings are removed and all others are trimmed.
I can do it in two steps:
final List tokens =
Lists.newArrayList(" some ", null, "stuff\t", "", " \nhere");
final Collection filtered =
Collections2.filter(
Collections2.transform(tokens, new Function(){
// This is a substitute for StringUtils.stripToEmpty()
// why doesn't Guava have stuff like that?
@Override
public String apply(final String input){
return input == null ? "" : input.trim();
}
}), new Predicate(){
@Override
public boolean apply(final String input){
return !Strings.isNullOrEmpty(input);
}
});
System.out.println(filtered);
// Output, as desired: [some, stuff, here]
But is there a Guava way of combining the two actions into one step?