Java 8 起为 Collection 集合新增了一个 removeIf(Predicate filter) 方法,该方法将会批量删除符合 filter 条件的所有元素。该方法需要一个 Predicate 对象作为参数,Predicate 也是函数式接口,因此可使用 Lambda 表达式作为参数。
示例使用 Predicate 来过滤集合。
public class ForeachTest {
public static void main(String[] args) {
// 创建一个集合
Collection objs = new HashSet();
objs.add(new String("中文百度搜索Java教程"));
objs.add(new String("中文百度搜索C++教程"));
objs.add(new String("中文百度搜索C语言教程"));
objs.add(new