Created
December 4, 2015 10:25
-
-
Save danshan/a0d83cdccbf0f77ae559 to your computer and use it in GitHub Desktop.
How to combine filter and transform for Guava?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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