I wanted to use Streams.intRange(int start, int end, int step) to achieve reverse ordered stream. However it seems that java.util.Streams class is no longer available (howev
There is indeed no such method anymore in the JDK; the next closest you could get is IntStream.range()
but that will only step one by one.
One solution here would be to implement your own Spliterator.OfInt
; for instance something like this (VERY CRUDE; can be improved!):
public final class StepRange
implements Spliterator.OfInt
{
private final int start;
private final int end;
private final int step;
private int currentValue;
public StepRange(final int start, final int end, final int step)
{
this.start = start;
this.end = end;
this.step = step;
currentValue = start;
}
@Override
public OfInt trySplit()
{
return null;
}
@Override
public long estimateSize()
{
return Long.MAX_VALUE;
}
@Override
public int characteristics()
{
return Spliterator.IMMUTABLE | Spliterator.DISTINCT;
}
@Override
public boolean tryAdvance(final IntConsumer action)
{
final int nextValue = currentValue + step;
if (nextValue > end)
return false;
action.accept(currentValue);
currentValue = nextValue;
return true;
}
}
You would then use StreamSupport.intStream() to generate your stream from an instance of the class above.