Guawa: jak połączyć filtr i przekształcić?
Mam kolekcję ciągów i chciałbym przekonwertować ją na kolekcję ciągów, które były puste lub null ciągi są usuwane, a wszystkie inne są przycięte.
Mogę to zrobić w dwóch krokach:
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]
Ale czy istnieje sposób łączenia dwóch działań w jeden krok?
47
Author: Sean Patrick Floyd, 2010-11-25
1 answers
W nadchodzącej najnowszej wersji(12.0) Guava pojawi się Klasa o nazwie FluentIterable .
Ta klasa zapewnia brakujące API fluent dla tego rodzaju rzeczy.
Używając FluentIterable, powinieneś być w stanie zrobić coś takiego:
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();
80
Author: Olivier Heidemann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-08 03:59:12
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-08 03:59:12