遍历List执行add/remove的坑与建议
相信大家肯定都碰到过这样的业务场景。现在有一组学生成绩List,需要筛选出成绩大于90分的名单,肯定有部分人会这么做,代码如下;
1、学生类
public class Student {
private Integer score;
private String name;
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(Integer score, String name) {
super();
this.score = score;
this.name = name;
}
@Override
public String toString() {
return "Student [score=" + score + ", name=" + name + "]";
}
}
2、测试代码
public // 测试类
class Test {
public static void main(String[] args) {
// 需求:初始化一批学生的成绩,然后筛选出成绩大于90分的人
List<Student> students = new ArrayList<>();
students.add(new Student(54, "小明"));
students.add(new Student(98, "小红"));
students.add(new Student(67, "小刚"));
students.add(new Student(120, "小芳"));
students.add(new Student(99, "小陈"));
students.add(new Student(89, "小张"));
students.add(new Student(90, "小李"));
students.add(new Student(100, "小王"));
students.add(new Student(54, "小强"));
students.add(new Student(95, "小刘"));
for(Student student: students) {
if(student.getScore() < 90) {
students.remove(student);
}
}
System.out.println(students);
}
}
你一定觉得这就OK了。很不幸,报异常了!
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at test01.java01.Test.main(Test.java:30)
这是为什么呢?因为foreach循环底层是依赖迭代器的,这里调用remove/add方法则直接将当前迭代器改动了,所以就类设计者主动抛出了并发异常。
3、解决方法
3.1、 用迭代器自身的remove/add方法实现操作
Iterator<Student> iterator = students.iterator();
while(iterator.hasNext()) {
if(iterator.next().getScore() < 90) {
iterator.remove();
}
}
3.2、 用for(i=0;i <list.size();i++)来实现操作
for(int i = 0; i < students.size(); i++) {
if(students.get(i).getScore() < 90) {
students.remove(i);
}
}
3.3、采用stream的filter实现
students = students.stream().filter(x -> x.getScore() >= 90).collect(Collectors.toList());
3.4、使用Collection的removeIf()方法来实现
students.removeIf(x -> x.getScore() < 90);
正常返回结果:
[Student [score=98, name=小红], Student [score=120, name=小芳], Student [score=99, name=小陈], Student [score=90, name=小李], Student [score=100, name=小王], Student [score=95, name=小刘]]