Today there is a requirement in the project to delete the object that satisfies the condition in the collection of a set type, and then think of course to call the set's remove (object O) method to delete the specified object and test the code:
public class Test {
public static void Main (string[] args) {
User User1 = new user ();
User1.setid (1);
User1.setname ("Zhangsan");
User User2 = new user ();
User2.setid (2);
User2.setname ("Lisi");
Set userset = new HashSet ();
Userset.add (user1);
Userset.add (User2);
for (Iterator it = Userset.iterator (); It.hasnext ();) {
User user = (user) it.next ();
if (user.getid () = = 1) {
Userset.remove (user);
}
if (user.getid () = = 2) {
User.setname ("Zhangsan");
}
}
for (Iterator it = Userset.iterator (); It.hasnext ();) {
User user = (user) it.next ();
System.out.println (User.getid () + "=" + User.getname ());
}
}
}
But when you run the program, you find something wrong:
Exception in thread "main" java.util.ConcurrentModificationException
At Java.util.hashmap$hashiterator.nextentry (Unknown Source)
At Java.util.hashmap$keyiterator.next (Unknown Source)
At Test. Test.main (test.java:23)
From the API, you can see that the implementation of the list and other collection is not synchronized, if there is simultaneous access in multi-threaded applications, and when the modification operation requires the external operation synchronization; The iterator object obtained by invoking the iterator operation is modified when the set is multithreaded Also automatically fails and throws Java.util.ConcurrentModificationException. This implementation mechanism is fail-fast, and the external modification does not provide any guarantee.
Web-based working mechanism on iterator. Iterator is working in a separate thread and has a mutex lock, meaning that iterator is not allowed to be iterated when it is working. When the iterator is created, a Memory index table (a single linked list) is established, which points to the original object, and when the original number of objects changes, the contents of the index table are not synchronized, so when the index pointer moves down, it cannot find the object to iterate, resulting in an error. List, set, etc. are dynamic, variable object number of data structure, but iterator is one-way immutable, only sequential reading, can not reverse the operation of the data structure, when the iterator point to the original data changes, iterator himself lost direction.
How can you meet the requirements and need to define a list to hold the objects that need to be deleted:
List dellist = new ArrayList ();
Finally, you only need to invoke the RemoveAll (Collection con) method of the collection.
public class Test {
public static void Main (string[] args) {
Boolean flag = false;
User User1 = new user ();
User1.setid (1);
User1.setname ("Shangsan");
User User2 = new user ();
User2.setid (2);
User2.setname ("Lisi");
Set userset = new HashSet ();
Userset.add (user1);
Userset.add (User2);
List dellist = new ArrayList ();
for (Iterator it = Userset.iterator (); It.hasnext ();) {
User user = (user) it.next ();
if (user.getid () = = 1) {
Dellist.add (user);
}
if (user.getid () = = 2) {
User.setname ("Zhangsan");
}
}
Userset.removeall (dellist);
for (Iterator it = Userset.iterator (); It.hasnext ();) {
User user = (user) it.next ();
System.out.println (User.getid () + "=" + User.getname ());
}
}
}
Objects that are iterated are not allowed to be modified during the iteration