private static class FilterByStringContains implements Predicate<String> {
private String filterString;
private FilterByStringContains(final String filterString) {
this.filterString = filterString;
}
@Override
public boolean apply(final String string) {
return string.contains(filterString);
}
}
I have a list of Strings, I want to filter it by the specified String so the returned value contains a list of only the specified strings. I was going to use a predicate as above but not sure how to apply this to filter a list
I’m assuming the
Predicatehere is from Guava? If so, you could useIterables.filter:Then build a list from that if you wanted:
… but I’d only suggest copying it to another list if you actually want it as a list. If you’re just going to iterate over it (and only once), stick to the iterable.