Iterators vs Iterable: Difference between revisions
From charlesreid1
(Created page with "In Java, there are two mechanisms to allow you to deal with iteration: * Iterable is a generic approach to getting an iterator. It enables an object to support for-each syntax...") |
No edit summary |
||
| Line 2: | Line 2: | ||
* Iterable is a generic approach to getting an iterator. It enables an object to support for-each syntax. It is not concerned with how the iterator works, where it starts or stops, etc. It just returns the iterator. If you implement Iterable<E> you just need to be able to provide an Iterator<E>. | * Iterable is a generic approach to getting an iterator. It enables an object to support for-each syntax. It is not concerned with how the iterator works, where it starts or stops, etc. It just returns the iterator. If you implement Iterable<E> you just need to be able to provide an Iterator<E>. | ||
* Iterator is a simple wrapper class (like a Scanner object) that just supports a few operations for getting the next item in an object/collection | * Iterator is a simple wrapper class (like a Scanner object) that just supports a few operations for getting the next item in an object/collection | ||
Also see [[Java/Iterable]] | |||
Revision as of 09:36, 25 June 2017
In Java, there are two mechanisms to allow you to deal with iteration:
- Iterable is a generic approach to getting an iterator. It enables an object to support for-each syntax. It is not concerned with how the iterator works, where it starts or stops, etc. It just returns the iterator. If you implement Iterable<E> you just need to be able to provide an Iterator<E>.
- Iterator is a simple wrapper class (like a Scanner object) that just supports a few operations for getting the next item in an object/collection
Also see Java/Iterable