2016-11-23 09:05:26 -08:00
|
|
|
// concurrent/ParallelStreamPuzzle.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2016-07-05 14:46:09 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-05 14:46:09 -06:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.*;
|
|
|
|
import java.util.stream.*;
|
|
|
|
|
|
|
|
public class ParallelStreamPuzzle {
|
|
|
|
static class IntGenerator
|
|
|
|
implements Supplier<Integer> {
|
|
|
|
private int current = 0;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public Integer get() {
|
2016-07-05 14:46:09 -06:00
|
|
|
return current++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
List<Integer> x =
|
|
|
|
Stream.generate(new IntGenerator())
|
|
|
|
.limit(10)
|
2021-01-31 15:42:31 -07:00
|
|
|
.parallel() // [1]
|
2016-07-05 14:46:09 -06:00
|
|
|
.collect(Collectors.toList());
|
|
|
|
System.out.println(x);
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 12:36:49 -08:00
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
2016-12-25 12:36:49 -08:00
|
|
|
*/
|