本文介绍了ArrayList 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 Java ArrayList 中过滤掉某些内容,例如:
How can you filter out something from a Java ArrayList like if you have:
- 你好吗
- 你好吗
- 乔
- 迈克
过滤器是如何"删除乔和迈克.
And the filter is "How" it will remove Joe and Mike.
推荐答案
可能最好的方法是使用 Guava
Probably the best way is to use Guava
List<String> list = new ArrayList<String>();
list.add("How are you");
list.add("How you doing");
list.add("Joe");
list.add("Mike");
Collection<String> filtered = Collections2.filter(list,
Predicates.containsPattern("How"));
print(filtered);
印刷品
How are you
How you doing
如果您想将过滤后的集合作为列表,您可以使用它(也来自 Guava):
In case you want to get the filtered collection as a list, you can use this (also from Guava):
List<String> filteredList = Lists.newArrayList(Collections2.filter(
list, Predicates.containsPattern("How")));
这篇关于ArrayList 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!