Chapter 1 HashSet class and interface Iterator of crazy Java handout

Source: Internet
Author: User
Tags concurrentmodificationexception

Chapter 1 Java collections of crazy Java Handouts

1: Exception in thread "main" java. lang. IllegalStateException

The source code is as follows:

 

Java. util. * Collection books = books. add ("lightweight Java Enterprise Application practices" books. add ("Crazy Java handout" books. add ("crazy Android handout" Iterator it =}

The output prompt of Edtiplus is as follows:

Exception in thread "main" java. lang. IllegalStateException
At java. util. HashMap $ HashIterator. remove (HashMap. java: 910)
At IteratorTest. main (IteratorTest. java: 12)

The remove Method in HashMap $ HashIterator is as follows:

                 (current ==                                 (modCount !=                               Object k =             current =              HashMap.             expectedModCount =         }

As shown in the above Code, the cause of the exception is current = null (Entry <K, V> current; // current entry)

Because it is an implemented Iterator interface, you can find the remove Method in the Iterator interface. If you want to call Iterator to traverse the Collection Object element to delete an element, you must use Object next () before,

Otherwise, this type of exception is thrown.

                               remove();

 

2: Exception in thread "main" java. util. ConcurrentModificationException

The main difference between the following two pieces of code is that the parameters in the 14th line code are different. For details, see: Which one will cause the above exception?

Java. util. * Collection books = books. add ("lightweight Java Enterprise Application practices" books. add ("Crazy Java handout" books. add ("crazy Android handout" Iterator it = String book = (book. equals ("crazy Android handouts "}

The output prompt of Editplus is as follows:

Exception in thread "main" java. util. ConcurrentModificationException
At java. util. HashMap $ HashIterator. nextEntry (HashMap. java: 894)
At java. util. HashMap $ KeyIterator. next (HashMap. java: 928)
At IteratorErrorTest. main (IteratorErrorTest. java: 14)

As shown above, I will list the relevant code:

           Entry<K,V> e =          (e ==  ?      }
   Entry<K,V>              (modCount != expectedModCount)                                Entry<K,V> e =              (e ==                                  ((next = e.next) ==                  Entry[] t =                  (index < t.length && (next = t[index++]) ==                current =                      }

From the above, we can know that the key cause of the exception is that when the remove () method in the Collecting class is used, the number of modCount sets is changed, but the expectedModCount in the corresponding Iterator is not modified.

In fact, the class annotations in HashMap indicate:

* <P> The iterators returned by all of this class's "collection view methods"
* Are <I> fail-fast </I>: if the map is structurally modified at any time after
* The iterator is created

Thus, in the face of concurrent
* Modification, the iterator fails quickly and cleanly, rather than risking
* Arbitrary, non-deterministic behavior at an undetermined time in
* Future.

The above code compares the remove () method in the above HashMapIterator with that in the HashMap to see the difference.

The answer to the difference between the two parameters in IteratorErrorTest is not the above explanation.

Modify the Code as follows:

               String book=                       }

In fact, we can see that "Crazy Java handout" is the last element in books. When "Crazy Java handout" is deleted, its Iterator has accessed the last element and its Field: next is already null, and the program jumps out of the entire loop.

 

Reference: http://www.blogjava.net/EvanLiu/archive/2008/08/31/224453.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.