"9.0" for the underlying analysis of iterators for Java collections

Source: Internet
Author: User

Foreword: If the traversal of the collection of Java (mainly hashmap in the keyset () and EntrySet () is how to value and can implement traversal) is not very clear, interested in-depth understanding of the small partners, this article can be used as a reference, due to the reason of time, It focuses on the core code of its traversal, the analysis of the underlying iterator. If the traversal of the collection has no basic understanding of the iterator, it is recommended to take a look at the relevant article, otherwise it is difficult to read this article. Several related posts are recommended:

1. I did not expect the series--HASHMAP implementation of the underlying details of the keyset,values,entryset of a bottom-level implementation Details
2.Java iterators in-depth understanding and use
iterator analysis of iterators in 3.Java set frame

Because the ArrayList iterator implementation is relatively simple, here does not repeat, mainly talk about the HashMap iterator, with the HashMap EntrySet () method as an example:

1. First look at the code and its running results:

If you open the Code of EntrySet (), you will find it magical, if you do not feel now, then you should look at this blog post "1. I didn't expect the series--hashmap to realize the bottom-level details of the keyset,values,entryset. (For convenience of description, refer to this blog post 1, other analogy). So now analysis, why EntrySet () can be implemented to take the value of the map to take out, now follow the steps to analyze the following (as the source of the view as a clue):

1.1 View HashMap Source, navigate to the EntrySet () method:

1.2 Notice that a EntrySet object is returned here, then navigate to the EntrySet object:

1.3 How do you go deep into the position you will find that there is no obvious return to the value of the map in the place, the specific reference to post 1, which is very clear, and then notice in the red circled place, notice that there is a iterator () method, which is the key, Please refer to post 2 and post 3 first to understand the following analysis. Here we know it must be that the map called the iterator so that the associated values in the map can be taken out (such as Set<map.entry<integer, integer>> set = Map.entryset (), and the value taken: [1=11, 2=22, 3=33]), but do not mistakenly assume that the set itself does not have any variables or that the field holds the value in the map, but rather in System.out.println (set), the set calls the ToString () method, and ToString () The iterator () method is called (called in a loop) to iterate through the values in the underlying array table (which does not understand itself) in the map, which is not very understandable. Let's first analyze the inheritance of the EntrySet object:

extends Abstractset<map.entry<k,v>> {...}


extends abstractcollection<e> implements set<e> {...}


abstractcollection<e> toString () {iterator<e> it = Iterator (); if (! It.hasnext ()) return "[]"; StringBuilder sb = new StringBuilder (); Sb.append (' ['); for (;;) {e E = It.next (); Sb.append (E = = this?) "(This Collection)": E), if (! It.hasnext ()) return Sb.append ('] '). toString (); Sb.append (', '). Append (');}}.


"9.0" for the underlying analysis of iterators for Java collections

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.