Using a custom Spliterator.OfDouble:
package com.logicbig.example.streamsupport;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.SplittableRandom;
import java.util.function.DoubleConsumer;
import java.util.stream.DoubleStream;
import java.util.stream.StreamSupport;
public class DoubleStreamExample3 {
public static void main(String... args) {
DoubleStream ds = StreamSupport
.doubleStream(createSpliterator(3), false);
ds.forEach(System.out::println);
}
private static Spliterator.OfDouble createSpliterator(int est) {
return new Spliterators.AbstractDoubleSpliterator(3, 0) {
double first = new SplittableRandom().nextDouble();
int c = 0;
@Override
public boolean tryAdvance(DoubleConsumer action) {
if (c++ == 3) {
return false;
}
action.accept(first + c);
return true;
}
};
}
}
Output
1.6408184134945287
2.6408184134945287
3.6408184134945287