Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created June 19, 2020 13:37
Show Gist options
  • Save SiAust/1c1641cabd00d8d04d624b59500154c6 to your computer and use it in GitHub Desktop.
Save SiAust/1c1641cabd00d8d04d624b59500154c6 to your computer and use it in GitHub Desktop.
Example custom iterator for a class.
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