Skip to content

Instantly share code, notes, and snippets.

@danshan
Created December 4, 2015 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danshan/a0d83cdccbf0f77ae559 to your computer and use it in GitHub Desktop.
Save danshan/a0d83cdccbf0f77ae559 to your computer and use it in GitHub Desktop.
How to combine filter and transform for Guava?
// In the latest version(12.0) of guava there will be a class named FluentIterable.
// This class provides the missing fluent API for this kind of stuff.
final Collection<String> filtered = FluentIterable
.from(tokens)
.transform(new Function<String, String>() {
@Override
public String apply(final String input) {
return input == null ? "" : input.trim();
}
})
.filter(new Predicate<String>() {
@Override
public boolean apply(final String input) {
return !Strings.isNullOrEmpty(input);
}
})
.toImmutableList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment