Java iterator iterator

Source: Internet
Author: User
Tags concurrentmodificationexception

Before we implemented the iterator pattern, many programming languages actually had built-in iterator classes, such as Java, which implemented the iterator iterator for us. We first look at the source code in iterator.

Through the JDK source we found that iterator is an interface that contains three methods: Hasnext, Next, and remove.

1  PackageJava.util;2 3  Public InterfaceIterator<e> {4 5     /**6 * Returns True if there are elements in the iterator7     */8     BooleanHasnext ();9 Ten     /** One * Returns the next element in the iterator A     */ - E Next (); -  the     /** - * Remove the lowest element in the collection by Iterators -     */ -     voidremove (); +}

Iterator is an interface, so how do you create an instance? Keep in mind that iterators and collection classes are very closely related, and we can create a iterator instance from a collection class, and ArrayList, LinkedList, and vector have implementations of it. Let's see how ArrayList creates an instance of an iterator iterator. Let's start by looking at the inheritance relationship between the collection and the iterator.

Because the relationship of the collection is relatively complex, here we mainly look at the comment section, by reading the source code will find that ArrayList Abstractlist abstract class iterator method and claim that the effect is better, and LinkedList is not overwrite, Thus, it can be judged that the iterator method of ArrayList is more efficient than the iterator method in LinkedList.

We look directly at the iterator method implemented in ArrayList.

1  Public Iterator<e> Iterator () {2     returnnew  Itr (); 3 }    

From the code, it returns a Itr object instance of the class, and follows the code to see what this ITR class is.

1 Private classItrImplementsIterator<e> {2     intCursor//returns the index of the next element3     intLastret =-1;//returns the index of the last element, or 1 if none4     intExpectedmodcount =Modcount;5 6      Public BooleanHasnext () {7         returnCursor! =size;8     }9 Ten@SuppressWarnings ("Unchecked") One      PublicE Next () { A checkforcomodification (); -         inti =cursor; -         if(I >=size) the             Throw Newnosuchelementexception (); -object[] Elementdata = ArrayList. This. Elementdata; -         if(I >=elementdata.length) -             Throw Newconcurrentmodificationexception (); +cursor = i + 1; -         return(E) Elementdata[lastret =i]; +     } A  at      Public voidRemove () { -         if(Lastret < 0) -             Throw Newillegalstateexception (); - checkforcomodification (); -  -         Try { inArrayList. This. Remove (Lastret); -cursor =Lastret; toLastret =-1; +Expectedmodcount =Modcount; -}Catch(Indexoutofboundsexception ex) { the             Throw Newconcurrentmodificationexception (); *         } $     }Panax Notoginseng  -     Final voidcheckforcomodification () { the         if(Modcount! =expectedmodcount) +             Throw Newconcurrentmodificationexception (); A     } the}

Originally ITR It is a private inner class that implements the iterator interface.

Let's read one line at a time. There is a Modcount variable in line 3rd. Follow this variable and find this variable a bit interesting:

protected transient int modcount = 0;

Found that there is a "transient" keyword, look up the data find this keyword means: Indicates that a domain is not part of the serialization of the object. This means that the variable is not included when the object is serialized, and we can leave a question as to why we should do so. (The JDK source note says so: The Modcount value, the iterator believes that the backing List should has. If this expectation was violated, the iterator has detected concurrent modification. English too can only read the last sentence: if the expectation is visibility, then the iterator detects a concurrent modification. Guessing is associated with concurrent multithreading. )

The implementation of Hasnext is simple:

1  Public Boolean Hasnext () {2     return Cursor! = size; Whether the index of the next element equals the size of ArrayList 3 }

Next's implementation:

 PublicE Next () {checkforcomodification (); Check if concurrent modificationsinti =cursor; if(I >=size)Throw Newnosuchelementexception (); Index greater than ArrayList size throws exception object[] Elementdata= ArrayList. This. Elementdata; The data in the ArrayList is actually taken from the back.if(I >=elementdata.length)Throw Newconcurrentmodificationexception (); Cursor= i + 1; return(E) Elementdata[lastret =i];}

In the next method we see a checkforcommodification method:

Final void checkforcomodification () {    if (modcount! = expectedmodcount        )Throw  New  concurrentmodificationexception ();}

It seems that this modcount variable is indeed associated with concurrency, and if the values of Expectedmodcount and Modcount are different, the exception that is currently being modified is thrown.

Finally, let's look at the implementation of the Remove method:

 Public voidRemove () {if(Lastret < 0)//This place is especially careful that you cannot call the Remove method directly without calling next, you must call the next method before the remove call, and assign its value to Lastret by the cursor index valueThrow Newillegalstateexception ();    Checkforcomodification (); Try{ArrayList. This. Remove (Lastret); Cursor=Lastret; Lastret=-1; Expectedmodcount=Modcount; } Catch(Indexoutofboundsexception ex) {Throw Newconcurrentmodificationexception (); }}

In the Remove method we should pay extra attention, in the first sentence is to detect whether Lastret is less than 0, we initialized the Lastret variable-1 value, which means that If we call the Remove method directly after creating the iterator instance to throw a IllegalStateException exception, how can we call it correctly? That is, call the Remove method before calling the next method, at which point the Lastreturn is assigned by the cursor index, and the Remove method can be used correctly. It also calls the Checkforcommodification method to do concurrent modification detection. In fact, we can see the JDK source code is written well, because it has done a lot of testing each method, to ensure that in as many scenarios as possible to run correctly. Today's Java iterator is a simple introduction through the JDK source code, through the source of reading can deepen our understanding, this is just a simple reading, and did not do a very deep understanding. Finally, we think of a iterator example ending.

1  PackageDay_29_iterator;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7 /**8  * @authorTurbo9  *Ten * September 29, 2016 One  */ A  Public classMain { -  -     /** the      * @paramargs -      */ -      Public Static voidMain (string[] args) { -List List =NewArrayList (); +List.add (1); -List.add (2); +Iterator Iterator =list.iterator (); A          while(Iterator.hasnext ()) { at System.out.println (Iterator.next ()); -         } -     } -  -}

Java iterator iterator

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.