这里使用的是Collection的removeIf方法,但是需要在匿名内部类Predicate中加入,否则就报错,如果add()方法中加入的事字符串,而不是new对象的话,就不用再内部类中加入<>了
public class Test {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add(new Person("sss",22));
coll.add(new Person("bbb",23));
coll.add(new Person("fff",24));
coll.add(new Person("aaa",22));
coll.removeIf(new Predicate() {
@Override
public boolean test(Person person) {
return person.getAge() ==24;
}
});
System.out.println(coll.toString());
}
}
class Person{
String name;
Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}