使用Java 8新增的Predicate操作Collection集合
Java 8 起为 Collection 集合新增了一个 removeIf(Predicate filter)
方法,该方法将会批量删除符合 filter 条件的所有元素。该方法需要一个 Predicate 对象作为参数,Predicate 也是函数式接口,因此可使用 Lambda 表达式作为参数。
如下程序示范了使用 Predicate 来过滤集合。
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
//创建一个集合
Collection objs=new HashSet();
objs.add("111111111");
objs.add("22222222");
objs.add("3333333");
objs.add("444444");
objs.add("55555");
objs.removeIf(ele -> ((String)ele).length()<=6);
System.out.println(objs);
}
}
上面程序中 objs.removeIf(ele -> ((String)ele).length()<=6);
调用了 Collection 集合的 removeIf() 方法批量删除集合中符合条件的元素,程序传入一个 Lambda 表达式作为过滤条件。所有长度小于6 的字符串元素都会被删除。编译、运行这段代码,可以看到如下输出:
[3333333, 111111111, 22222222]
使用 Predicate 可以充分简化集合的运算,假设 objs 集合,程序有如下三个统计需求:
- 统计集合中出现“英雄联盟”字符串的数量。
- 统计集合中出现“养爹人”字符串的数量。
- 统计集合中出现字符串长度大于 7 的数量。
此处只是一个假设,实际上还可能有更多的统计需求。如果采用传统的编程方式来完成这些需求,则需要执行三次循环,但采用 Predicate 只需要一个方法即可。下面代码示范了这种用法。
import java.util.Collection;
import java.util.HashSet;
import java.util.function.Predicate;
public class Test {
public static int calAll(Collection books,Predicate p){
int total=0;
for(Object obj:books){
// 使用Predicate的test()方法判断该对象是否满足Predicate指定的条件
if(p.test(obj)){
total++;
}
}
return total;
}
public static void main(String[] args) {
//创建一个集合
Collection objs=new HashSet();
objs.add("英雄联盟佐伊");
objs.add("英雄联盟拉克丝");
objs.add("英雄联盟万花通灵");
objs.add("英雄联盟时光守护者");
objs.add("英雄联盟峡谷养爹人");
// 统计集合中出现“英雄联盟”字符串的数量
System.out.println(calAll(objs, ele -> ((String) ele).contains("英雄联盟")));
// 统计集合中出现“养爹人”字符串的数量
System.out.println(calAll(objs, ele -> ((String)ele).contains("养爹人")));
// 统计集合中出现字符串长度大于 7 的数量
System.out.println(calAll(objs, ele -> ((String) ele).length() > 7));
}
}
输出结果为:
5 1 3
上面程序先定义了一个 calAll() 方法,它使用 Predicate 判断每个集合元素是否符合特定条件,条件将通过 Predicate 参数动态传入。程序传入了 3 个 Lambda 表达式,其目标类型都是 Predicate,这样 calAll() 方法就只会统计满足 Predicate 条件的图书。