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 |
||
| (One intermediate revision by the same user not shown) | |||
| 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 | ||
See [[Java/Iterable]] and [[Java/Iterator]] for examples. | |||
Latest revision as of 00:52, 26 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
See Java/Iterable and Java/Iterator for examples.