Created
June 19, 2020 13:37
-
-
Save SiAust/1c1641cabd00d8d04d624b59500154c6 to your computer and use it in GitHub Desktop.
Example custom iterator for a class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Range implements Iterable<Long> { | |
| private final long fromInclusive; | |
| private final long toExclusive; | |
| public Range(long from, long to) { | |
| this.fromInclusive = from; | |
| this.toExclusive = to; | |
| } | |
| @Override | |
| public Iterator<Long> iterator() { | |
| return new Iterator<>() { | |
| private long pos = fromInclusive; | |
| @Override | |
| public boolean hasNext() { | |
| return toExclusive - pos > 0; | |
| } | |
| @Override | |
| public Long next() { | |
| return pos++; | |
| } | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment