For (INT I = 0; I <list. Size (); I ++) {// This method does not affect the addition or deletion of a set when traversing the set.
// List. Remove (list. Get (I ));
}
When performing iterative traversal on a map and deleting some key-value pairs that meet the conditions
Java. util. concurrentmodificationexception.
Exception in thread "Main" Java. util. concurrentmodificationexception
At java. util. hashmap $ hashiterator. nextentry (hashmap. Java: 787)
At java. util. hashmap $ keyiterator. Next (hashmap. Java: 823)
The solution is as follows:
Import java. util .*;
Public class testmap {
Public testmap (){
}
Public static void main (string [] ARGs ){
Java. util. Map M = new java. util. hashmap ();
M. Put ("AAA", "lizongbo ");
M. Put ("BBB", "lizongbo ");
M. Put ("CCC", "lizongbo ");
M. Put ("DDD", "lizongbo ");
M. Put ("eee", "lizongbo ");
M. Put ("fff", "lizongbo ");
M. Put ("ggg", "lizongbo ");
M. Put ("adads", "lizongbo ");
M. Put ("dffd", "lizongbo ");
M. Put ("dcxv", "lizongbo ");
M. Put ("lizongbo", "lizongbo ");
M. Put ("ERT", "lizongbo ");
M. Put ("544", "lizongbo ");
Iterator = M. keyset (). iterator ();
While (iterator. hasnext ()){
String sessionid = (string) iterator. Next ();
If ("ggg". Equals (sessionid) | "lizongbo". Equals (sessionid) |
& Quot; 544 & quot;. Equals (sessionid )){
Iterator. Remove (); // This line of code is critical.
M. Remove (sessionid );
}
}
System. Out. println (M. Get ("ggg "));
System. Out. println (M. Get ("lizongbo "));
System. Out. println (M. Get ("544 "));
}