Guava: how to combine a filter and transform?

I have a rowset and I would like to convert it to a row collection, all were empty or empty. The rows are deleted, and all the rest are trimmed.

I can do this in two steps:

final List<String> tokens = Lists.newArrayList(" some ", null, "stuff\t", "", " \nhere"); final Collection<String> filtered = Collections2.filter( Collections2.transform(tokens, new Function<String, String>(){ // 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<String>(){ @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 way for Guava to combine two actions in one step?

+46
java guava
Nov 25 '10 at 13:47
source share
1 answer

In the upcoming latest version (12.0) of Guava, there will be a class called FluentIterable . This class provides the missing free API for this kind of material.

Using FluentIterable, you should do something like this:

 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(); 
+77
Apr 26 2018-12-12T00:
source share
β€” -



All Articles