Java Collection _ Talking about the difference between iterable and iterator _java

Source: Internet
Author: User
Tags iterable

1. iterator is an iterator class, and iterable is designed to iterate by using foreach as long as the interface is implemented.

2. the iterator interface is encapsulated in iterable, and iterator iterators can be used as long as the class of the Iterable interface is implemented.

3. Collection collection, List, and set are all implementation classes of iterable, so they and their subclasses can use foreach for iteration.

4. Iterator Neutralization Core method next (), Hasnext (), remove (), are all dependent on the current position, if the collection directly implements iterator, you must include a pointer to the current iteration position. When a collection is passed between methods, the value after next () is not known because the current position is unknown. However, when implementing Iterable, each call returns an iterator from the beginning, and each iterator does not affect each other.

The above Java collection _ talking about the difference between iterable and iterator is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.