Traverse the list of pits and suggestions for performing add/remove

Traverse the list of pits and suggestions for performing add/remove

I believe everyone must have encountered such business scenarios. Now there is a list of student scores, and you need to filter out the list with scores greater than 90. Certain people will do this. The code is as follows;

1. Student

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. Test code

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);
	}

}

You must think this is OK. Unfortunately, an exception was reported!
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)
Why is this? Because the bottom layer of the foreach loop relies on iterators, calling the remove/add method here directly changes the current iterator, so the class designer actively throws a concurrent exception.

3. Solution

3.1. Use the remove/add method of the iterator to implement the operation
Iterator<Student> iterator = students.iterator();
		while(iterator.hasNext()) {
    
    
			if(iterator.next().getScore() < 90) {
    
    
				iterator.remove();
			}
		}
3.2. Use for(i=0;i <list.size();i++) to realize the operation
for(int i = 0; i < students.size(); i++) {
    
    
			if(students.get(i).getScore() < 90) {
    
    
				students.remove(i);
			}
		}
3.3. Implementation of filter using stream
students = students.stream().filter(x -> x.getScore() >= 90).collect(Collectors.toList());
3.4. Use the removeIf() method of Collection to achieve
students.removeIf(x -> x.getScore() < 90);

Normal return result:

[Student [score=98, name=小红], Student [score=120, name=小芳], Student [score=99, name=小陈], Student [score=90, name=小李], Student [score=100, name=小王], Student [score=95, name=小刘]]

Guess you like

Origin blog.csdn.net/m0_53085735/article/details/112989012