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);
}
}
我有一个字符串列表,我想按指定的字符串对其进行过滤,以便返回的值只包含指定字符串的列表。我打算使用如上所述的谓词,但不确定如何应用它来过滤列表
发布于 2012-03-26 18:44:39
我猜这里的Predicate
来自Guava?如果是这样,您可以使用Iterables.filter
Iterable<String> filtered = Iterables.filter(original, predicate);
然后,如果你想要的话,建立一个列表:
List<String> filteredCopy = Lists.newArrayList(filtered);
..。但我只建议将它复制到另一个列表中,如果你真的想要它作为一个列表。如果你只打算迭代它(并且只迭代一次),那么坚持使用iterable。
https://stackoverflow.com/questions/9877780
复制相似问题