Created
August 17, 2015 13:50
-
-
Save Dierk/7cb5e139ebc093d9cc45 to your computer and use it in GitHub Desktop.
an impure parallel stream that should only map but actually serves as random number generator
This file contains hidden or 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.*; | |
import java.util.function.*; | |
import java.util.stream.*; | |
public class ParallelStreamTest { | |
static class IntGenerator implements Supplier<Integer> { | |
private int current = 0; | |
public Integer get() { | |
return current++; | |
} | |
} | |
public static final void main(String[] args) { | |
Stream<Integer> s = Stream.generate(new IntGenerator()); | |
List<Integer> xs = s | |
.limit(10) | |
.parallel() | |
.map(x -> x * 2) | |
.collect(Collectors.toList()); | |
System.out.println(xs); | |
} | |
} | |
// Kudos: Volker Steiss |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment