Java container (eight)---Java iterator (iterator) (and the difference between and for loops) __java

Source: Internet
Author: User
Tags iterable
An iterator is a pattern that allows the traversal behavior of a data structure of a sequence type to be separated from the object being traversed, that is, we do not need to care what the underlying structure of the sequence is like. Once you get this object, you can iterate through the object's interior using an iterator.

1.Iterator

Java provides a dedicated iterator <<interface>>iterator, and we can implement the interface for a sequence to provide a standard Java iterator. The function of the iterator interface is to "use" an iterator.

Document definition: [Java] view plain copy print? Package Java.util;
Publicinterface iterator<e> {
Boolean hasnext ()//To determine if the next object element exists
E next ();
void Remove (); } [Java] view plain copy print? <span style= "FONT-SIZE:18PX;"       >package Java.util;          Public interface Iterator<e> {Boolean hasnext ();//To determine whether the next object element E next () exists;   void Remove (); } </span>


2.Iterable

Java also provides a iterable interface, the Iterable interface is implemented after the function is "return" an iterator, we commonly implement the interface of the sub-interface are: collection<e>, Deque<e>, list<e , Queue<e>, set<e>, etc. the iterator () method of the interface returns a standard iterator implementation. Implementing this interface allows an object to become the target of a Foreach statement. You can traverse your underlying sequence through the foreach syntax.

The Iterable interface contains a iterator () method that can produce iterator, and the Iterable interface is used by foreach to move through the sequence. Therefore, if you create a class that implements the Iterable interface, you can use it in a foreach statement. [Java] view plain copy print? Document definition:
Package Java.lang;
Import Java.util.Iterator; Public interface Iterable<t> {iterator<t> iterator ();} [Java] view plain copy print? <span style= "FONT-SIZE:18PX;"       > Document definition: Package Java.lang;    Import Java.util.Iterator;    Public interface Iterable<t> {iterator<t> iterator (); } </span>


[Java] view plain copy Print

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.