Remove some elements from list and set
How the error code is written:
New Hashset<string>(); Set.add ("aaaaaa"); Set.add ("bbbbbb"); Set.add ("CCCCCC"); Set.add ("dddddd"), Set.add ("eeeeee"), Set.add ("FFFFFF"), Set.add ("GGGGGG" ); for (String str:set) { if ("dddddd". Equals (str)) { set.remove (str); }}
Operation Error:
Exception in thread "main" java.util.ConcurrentModificationException at java.util.hashmap$ Hashiterator.nextentry (Hashmap.java:793) at java.util.hashmap$keyiterator.next (Hashmap.java: 828) at Test2. Test1.main (Test1.java:16)
You cannot add or delete data from a set when you are traversing it as above
The correct code to be spelled
Set<string> set =NewHashset<string>(); Set.add ("AAAAAA"); Set.add ("BBBBBB"); Set.add ("CCCCCC"); Set.add ("DDDDDD"); Set.add ("Eeeeee"); Set.add ("FFFFFF"); Set.add ("GGGGGG"); Iterator<String> it =set.iterator (); while(It.hasnext ()) {String str=It.next (); if("DDDDDD". Equals (str)) {It.remove (); }} for(String str:set) {System.out.println (str);}
Output:
Aaaaaaffffffeeeeeebbbbbbccccccgggggg
Java loop traversal deletes the elements in the set list