Today, when doing a project, you need to delete some elements in the list and set, then use the Edge traversal, edge Delete method, but reported the following exception:
Concurrentmodificationexception
In order not to forget later, use the rotten pen to record it as follows:
The writing of the error code, that is, to quote the above exception:
set<checkwork> set = This.getuserdao (). GETALL (QF). Get (0). Getactioncheckworks ();
for (Checkwork checkwork:set) {
if (checkwork.getstate () ==1) {
set.remove (checkwork);
}
}
Note: Using the above notation will report the above Concurrenmodificationexception exception, because the collection can not be removed while traversing.
The correct wording is as follows:
1. Traverse Delete List
list<checkwork> list = This.getuserdao (). GetAll ();
iterator<checkwork> chk_it = List.iterator ();
while (Chk_it.hasnext ()) {
Checkwork checkwork = Chk_it.next ();
if (Checkwork.getplanstate () ==1) {
chk_it.remove ();
}
}
2. Traversal Delete Set
set<checkwork> set = This.getuserdao (). GetAll (). Get (0). Getactioncheckworks ();
Iterator<checkwork> it = Set.iterator ();
while (It.hasnext ()) {
Checkwork checkwork = It.next ();
if (Checkwork.getstate () ==1) {
it.remove ();
}
}
The above in the Java loop to delete the list and set of sets of elements in the set of methods (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.