ArrayList的过滤器 [英] ArrayList filter

查看:559
本文介绍了ArrayList的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何从一个Java的ArrayList过滤掉类似,如果您有:

How can you filter out something from a Java ArrayList like if you have:


  1. 您是如何

  2. 您怎么样


  3. 迈克

和过滤器是如何将删除乔和迈克。

And the filter is "How" it will remove Joe and Mike.

推荐答案

也许最好的方法是使用番石榴

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

如果你想要得到的过滤集合作为一个列表,你可以使用这个(也来自番石榴):

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的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆