Skip to content

Instantly share code, notes, and snippets.

@timyates
Created October 20, 2015 12:43
Show Gist options
  • Save timyates/074bd748b75832bf90c0 to your computer and use it in GitHub Desktop.
Save timyates/074bd748b75832bf90c0 to your computer and use it in GitHub Desktop.
Infinite Spliterator? I have no idea if this is right... Looks right, but there are many opportunities for wrong...
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.IntConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ParallelStreamTest {
static class IntGenerator implements Supplier<Integer> {
private int current = 0;
public Integer get() {
return current++;
}
public Spliterator<Integer> spliterator() {
return new InternalSpliterator();
}
private class InternalSpliterator extends Spliterators.AbstractIntSpliterator {
protected InternalSpliterator() {
super(Long.MAX_VALUE, ORDERED | NONNULL);
}
@Override
public boolean tryAdvance(IntConsumer action) {
action.accept(get());
return true;
}
}
}
public static void main(String[] args) {
Stream<Integer> s = StreamSupport.stream(new IntGenerator().spliterator(), true);
List<Integer> xs = s
.parallel()
.limit(10)
.map(x -> x * 2)
.collect(Collectors.toList());
System.out.println(xs);
}
}
@timyates
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment