Created
October 20, 2015 12:43
-
-
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...
This file contains 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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From https://gist.github.com/Dierk/7cb5e139ebc093d9cc45