Basic knowledge of Java Collection Framework Notes 11:collection collection iterator principle and source code analysis

Source: Internet
Author: User
Tags iterable

1. Why is an iterator not defined as a class but defined as an interface?

A: Suppose the iterator defines a class so that we can create an object of that class and invoke the method of that class to implement the traversal of the collection. But what? We think that there are a lot of collection classes available in Java, and the data structures of these collection classes are different, so the way you store them and the way you traverse them should be different. In turn, they should not traverse in the same way, and eventually, there is no iterator class defined.

And no matter what kind of collection you are, you should have access to the elements, and it is best to assist in judging the function, so that before acquiring, the first judgment. That way, it's not easy to make mistakes. That is, the judgment function and the acquisition function should be a collection traversal, and each kind of collection is not the same way, so we take these two functions to extract, do not provide a concrete implementation, this is the interface.

2. So where is the real implementation class?

A: In a real specific subclass, it is embodied in the way of the inner class.

3. The source of the iterator:

1  Public InterfaceIterator {2     BooleanHasnext ();3 Object Next ();4 }5 6  Public Interfaceiterable {7 Iterator Iterator ();8 }9 Ten  Public InterfaceCollectionextendsiterable { One Iterator Iterator (); A } -  -  Public InterfaceListextendsCollection { the Iterator Iterator (); - } -  -  Public classArrayListImplementsList { +      PublicIterator Iterator () { -         return NewItr (); +     } A      at     Private classItrImplementsIterator { -          Public BooleanHasnext () {} -          PublicObject Next () {} -     } - } -  in  -Collection C =NewArrayList (); toC.add ("Hello"); +C.add ("World"); -C.add ("Java"); theIterator it = C.iterator ();//new Itr (); *  while(It.hasnext ()) { $String s =(String) It.next ();Panax Notoginseng System.out.println (s); -}

Basic knowledge of Java Collection Framework Notes 11:collection collection iterator principle and source code analysis

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.