java8 新特性Lambda表达式之RemoveIf

public class Event_RemoveIf {

public static void main(String[] ags) {

Listpersons=new ArrayList<>();

persons.add(new Person("Lily",18));

persons.add(new Person("Poliy",20));

persons.add(new Person("LiHui",15));

persons.add(new Person("Lucy",13));

//需求:删除集合中未成年的小学生

/** //第一种实现方式:

ListIterator itS=persons.listIterator();

while(itS.hasNext()) {

Person p=itS.next();

if(p.age<18) {

itS.remove();

}

}

System.out.println(persons);

//输出:

//[Person [name=Lily, age=18], Person [name=Poliy, age=20]] **/

 

//第二种实现方式

//default boolean removeIf(Predicate filter) {} 方法中  有一个Predicate参数  这个参数是一个函数式接口boolean test(T t);  你又懂了 哈哈

//需求:删除集合中未成年的小学生

persons.removeIf(ele -> ele.age < 18);

System.out.println(persons);

//输出:

//[Person [name=Lily, age=18], Person [name=Poliy, age=20]]

}

}

你可能感兴趣的:(java8,新特性Lambda表达式)