Recently encountered a problem, to delete a collection of an element, how to solve?
I wrote two pieces of code to delete, all reported a mistake java.util.ConcurrentModificationException; in order to make it more likely that the point of the program was not noticed, ape, I'm here to share the following ~
Look at the code first
1 Packagecom.zwt1234;2 3 ImportJava.util.HashSet;4 ImportJava.util.Iterator;5 ImportJava.util.Set;6 7 Public classIteratortest {8 Public Static voidMain (string[] args) {9set<object> s =NewHashset<object> (0);TenS.add (NewMan ("Xiaoming", 12)); OneS.add (NewMan ("Xiao Li", 15)); AS.add (NewMan ("Xiao Hua", 21)); - Validateiterator (s); - //Validateforeach (s); the } - - /** - * methods to verify the iterator interface + * iterator into the loop, the equivalent of a lock on the thread, no one let go, can only use iterator own objects to manipulate the data - * So the following with S.remove (man) is, program error + * @params A */ at Public Static voidValidateiterator (set<object>s) { -Iterator<object> iter =s.iterator (); - while(Iter.hasnext ()) { -Man mans =(man) Iter.next (); - System.out.println (man); - if(Man.name.equals ("Xiao Li"))) { in Iter.remove (); - //S.remove (man);//at this time the program error java.util.ConcurrentModificationException to } + } -System.out.println ("------------------------"); the /* * * After performing the above delete operation, reprint the elements in the S collection, verify the Operation $ */Panax NotoginsengITER =s.iterator (); - while(Iter.hasnext ()) { the System.out.println (Iter.next ()); + } A } the + /** - * Verify the Foreach Loop $ * The Foreach loop is more convenient than the iterator on the query, because the code quantity is less; $ * But it's more than iterator on a year old data operation - * @params - */ the Public Static voidValidateforeach (set<object>s) { - for(Object obj:s) {WuyiMan mans =(man) obj; the System.out.println (man); - if(Man.name.equals ("Xiao Li"))) { WuS.remove (man);//This is also not allowed to be deleted, because the Foreach loop is the bottom of the iterator implementation method - } About } $ } - } - - classMan { A String name; + intAge ; the -Man (String name,intAge ) { $ This. Name =name; the This. Age =Age ; the } the the @Override - PublicString toString () { in return"Man [name=" + name + ", age=" + Age + "]"; the } the}
View Code
Let's analyze the code below.
1. First I use,iterator to achieve the deletion , at the beginning, in the traversal to the object stored in the name of Xiao Li, I let this object, in the set set of the Delete, in the IF statement, in the face of 2 questions, set S has provided us with a remove ( ) method, and iterator also gives us a remove () method, which one do we use?
At the very beginning, I chose the set to set the method , but in the run, the result is an error , and instead of using the method provided by iterator can be run correctly , the normal deletion of the object I need to delete.
Summary of results : When using iterator iterators, we need to delete the data in the collection, using the iterator Remove () method, because when using iterator, it is equivalent to giving the thread a lock on the resource, Only iterator itself can delete the data, and the collection thread is unable to delete the resource while the iterator occupies the resource.
2. The same foreach enhancement for loop implementation is removed when I also go to implement the next.
Because the Foreach loop does not provide us with the Remove () method, we directly use the Delete method provided by the set set, and we find that the same error is reported and iterator, so it proves that the foreach loop is not suitable for data deletion. .
(For the result of the above code operation, please copy it, directly run to see the results, I will not show here)
3. Final Summary : ① compared to foreach, the use of iterators iterator is slightly cumbersome;
② compared to foreach, the iterator Iterato is clearly more flexible in manipulating the data than the foreach.
Finally, the above summary of knowledge Bo master his own personal views, there is not thoughtful place, or the wrong place, please correct me, thank you ~
Traversing loops iterator and Foreach loops in the Java container