java.util.ConcurrentModificationException异常;java.util.ConcurrentModificationException实战

写代码遇到这个问题,很多博客文章都是在反复的强调理论,而没有对应的实例,所以这里从实例出发,后研究理论:

一、错误产生情况

1 、字符型

   (1)添加

 public static void main(String[] args) {

        List<String> stringList = new ArrayList<String>();
        stringList.add("张三毛");
        stringList.add("李四");
        stringList.add("王五");
        stringList.add("钱二");

        if (stringList!=null) {
            if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())){
                for (String s : stringList) {
                    stringList.add("赵大");
                }
            }
        }

        System.out.println(stringList);

 }  

报错

改写为如下即可:

 1  public static void main(String[] args) {
 2 
 3         List<String> stringList = new ArrayList<String>();
 4         stringList.add("张三毛");
 5         stringList.add("李四");
 6         stringList.add("王五");
 7         stringList.add("钱二");
 8 
 9         if (stringList != null) {
10             if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) {
11 
12                 stringList.add("赵大");
13 
14                 Iterator<String> it = stringList.iterator();
15                 while (it.hasNext()) {
16                       it.next();
17                 }
18             }
19         }
20 
21         System.out.println(stringList);
22 
23     }
View Code

打印出结果:

   

然后我们打印其next,就会发现其循环就是通过it.next()方法将数据添加进去的

打印:

  (2)、删除

错误写法:

 1   private static String key = "钱二";
 2 
 3 
 4     public static void main(String[] args) {
 5 
 6         List<String> stringList = new ArrayList<String>();
 7         stringList.add("张三毛");
 8         stringList.add("李四");
 9         stringList.add("王五");
10         stringList.add("钱二");
11 
12         if (stringList != null) {
13             if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) {
14 
15                 for (String s : stringList) {
16                     if (key.equals(s)){
17                         stringList.remove(s);
18                     }
19                 }
20             }
21         }
22 
23         System.out.println(stringList);
24 
25     }
View Code

报错:

改写为:

 1  private static String key = "钱二";
 2 
 3 
 4     public static void main(String[] args) {
 5 
 6         List<String> stringList = new ArrayList<String>();
 7         stringList.add("张三毛");
 8         stringList.add("李四");
 9         stringList.add("王五");
10         stringList.add("钱二");
11 
12         if (stringList != null) {
13             if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) {
14                 Iterator<String> it = stringList.iterator();
15 
16                 while (it.hasNext()) {
17                     String next = it.next();
18 
19                     if (key.equals(next)) {
20                         it.remove();
21                     }
22 
23                 }
24 
25             }
26         }
27 
28         System.out.println(stringList);
29 
30     }
View Code

结果:

      2、整形

  正确添加:

 1 public class ConcurrentBaseApplication {
 2 
 3     private static String key = "钱二";
 4 
 5 
 6     public static void main(String[] args) {
 7 
 8         List<Integer> integerList = new ArrayList<Integer>();
 9         integerList.add(1);
10         integerList.add(2);
11         integerList.add(3);
12         integerList.add(4);
13 
14         Integer next=0;
15 
16         if (integerList != null) {
17             if (!integerList.equals(Collections.EMPTY_LIST.isEmpty())) {
18                 integerList.add(5);
19 
20                 Iterator<Integer> it = integerList.iterator();
21 
22                 while (it.hasNext()) {
23                     next = it.next();
24                     System.out.println(next);
25                 }
26 
27             }
28         }
29 
30         System.out.println(integerList);
31 
32     }
33 
34 }
View Code

结果:

正确删除:

 1 public class ConcurrentBaseApplication {
 2 
 3     public static void main(String[] args) {
 4 
 5         List<Integer> integerList = new ArrayList<Integer>();
 6         integerList.add(1);
 7         integerList.add(2);
 8         integerList.add(3);
 9         integerList.add(4);
10 
11         if (integerList != null) {
12             if (!integerList.equals(Collections.EMPTY_LIST.isEmpty())) {
13 
14                 Iterator<Integer> it = integerList.iterator();
15 
16                 while (it.hasNext()) {
17                     Integer  next = it.next();
18                     if("2".equals(next.toString())){
19                        it.remove();
20                     }
21                 }
22             }
23         }
24 
25         System.out.println(integerList);
26 
27     }
28 
29 }
View Code

 结果:

 (3) 实体类

创建实体类

Student

package com.north.big.penguin.pojo;

import java.io.Serializable;

/**
 * @author liuyangos8888
 */
public class Student implements Serializable {

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private String age;

    /**
     * 标识
     */
    private String id;

    public Student() {
    }

    public Student(String name, String age, String id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", id='" + id + '\'' +
                '}';
    }
}

 正确的添加:

 1 public class ConcurrentBaseApplication {
 2 
 3     public static void main(String[] args) {
 4 
 5         List<Student> students = new ArrayList<Student>();
 6 
 7         Student student1 = new Student();
 8         student1.setName("李雷");
 9         student1.setAge("13");
10         student1.setId(UUID.randomUUID().toString());
11 
12         Student student2 = new Student();
13         student2.setName("韩梅梅");
14         student2.setAge("14");
15         student2.setId(UUID.randomUUID().toString());
16 
17         Student student3 = new Student();
18         student3.setName("李华");
19         student3.setAge("15");
20         student3.setId(UUID.randomUUID().toString());
21 
22 
23         students.add(student1);
24         students.add(student2);
25         students.add(student3);
26 
27         if (students != null) {
28             if (!students.equals(Collections.EMPTY_LIST.isEmpty())) {
29 
30                 Student student4 = new Student();
31                 student4.setName("小明");
32                 student4.setAge("16");
33                 student4.setId(UUID.randomUUID().toString());
34 
35 
36                 Iterator<Student> it = students.iterator();
37 
38                 while (it.hasNext()) {
39                     // 添加学生
40                     Student  next = it.next();
41                 }
42             }
43         }
44 
45         System.out.println(students);
46 
47     }
48 
49 }
View Code

结果:

  正确的删除:

 1 public class ConcurrentBaseApplication {
 2 
 3     public static void main(String[] args) {
 4 
 5         List<Student> students = new ArrayList<Student>();
 6 
 7         Student student1 = new Student();
 8         student1.setName("李雷");
 9         student1.setAge("13");
10         student1.setId(UUID.randomUUID().toString());
11 
12         Student student2 = new Student();
13         student2.setName("韩梅梅");
14         student2.setAge("14");
15         student2.setId(UUID.randomUUID().toString());
16 
17         Student student3 = new Student();
18         student3.setName("李华");
19         student3.setAge("15");
20         student3.setId(UUID.randomUUID().toString());
21 
22 
23         students.add(student1);
24         students.add(student2);
25         students.add(student3);
26 
27         if (students != null) {
28             if (!students.equals(Collections.EMPTY_LIST.isEmpty())) {
29 
30                 Student student4 = new Student();
31                 student4.setName("小明");
32                 student4.setAge("16");
33                 student4.setId(UUID.randomUUID().toString());
34 
35 
36                 Iterator<Student> it = students.iterator();
37 
38                 while (it.hasNext()) {
39                     // 添加学生
40                     Student  next = it.next();
41 
42                     Integer integerAge = Integer.valueOf(next.getAge());
43 
44                     if(integerAge>14){
45                         it.remove();
46                     }
47                 }
48             }
49         }
50 
51         System.out.println(students);
52 
53     }
54 
55 }
View Code

结果:

结果集:

1 [Student{name='李雷', age='13', id='617e914f-ed33-472d-bbbd-1a6bf5ef5901'}, Student{name='韩梅梅', age='14', id='cb804e43-4846-4fc6-84c8-9e4a6b17d7f1'}]
View Code

猜你喜欢

转载自www.cnblogs.com/liuyangfirst/p/11223570.html
今日推荐