use of java.util.stream.IntStream in project cradle by BingLau7.
the class TestStream method main.
public static void main(String[] args) {
int[] numbers = new int[] { 1, 2, 3, 4, 5, 6 };
// 记住区分惰性求值或是及早求值
IntStream ns = Arrays.stream(numbers).filter(i -> i < 3);
ns.forEach(x -> System.out.print(x + " "));
System.out.println();
List<String> collected = Stream.of("a", "b", "c").collect(Collectors.toList());
System.out.println(collected);
List<String> collected2 = Stream.of("a", "b", "hello").map(string -> string.toUpperCase()).collect(Collectors.toList());
System.out.println(collected2);
int count = Stream.of(1, 2, 3).reduce(0, (acc, element) -> acc + element);
System.out.println(count);
}
use of java.util.stream.IntStream in project jdk8u_jdk by JetBrains.
the class BitSetStreamTest method testRandomStream.
@Test
public void testRandomStream() {
final int size = 1024 * 1024;
final int[] seeds = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 };
final byte[] bytes = new byte[size];
for (int seed : seeds) {
final Random random = new Random(seed);
random.nextBytes(bytes);
final BitSet bitSet = BitSet.valueOf(bytes);
final int cardinality = bitSet.cardinality();
final IntStream stream = bitSet.stream();
final int[] array = stream.toArray();
assertEquals(array.length, cardinality);
int nextSetBit = -1;
for (int i = 0; i < cardinality; i++) {
nextSetBit = bitSet.nextSetBit(nextSetBit + 1);
assertEquals(array[i], nextSetBit);
}
}
}
use of java.util.stream.IntStream in project TeeTime by teetime-framework.
the class ProducerConsumerGlobalTaskPoolIT method shouldExecuteProducerConsumer.
private void shouldExecuteProducerConsumer(final int numThreads, final int numExecutions) {
int numElements = 10_000;
List<Integer> processedElements = new ArrayList<>();
IntStream inputElements = IntStream.iterate(0, i -> i + 1).limit(numElements);
Configuration config = new Configuration().from(new StreamProducer<>(inputElements)).end(new CollectorSink<>(processedElements));
TeeTimeScheduler scheduling = new GlobalTaskPoolScheduling(numThreads, config, numExecutions);
Execution<Configuration> execution = new Execution<>(config, true, scheduling);
execution.executeBlocking();
for (int i = 0; i < numElements; i++) {
assertThat(processedElements.get(i), is(i));
}
assertThat(processedElements, hasSize(numElements));
}
use of java.util.stream.IntStream in project TeeTime by teetime-framework.
the class ThreeStagesGlobalTaskPoolIT method shouldExecutePipelineCorrectlyManyElements.
private void shouldExecutePipelineCorrectlyManyElements(final int numElements, final int numThreads, final int numExecutions) {
List<Integer> processedElements = new ArrayList<>();
IntStream inputElements = IntStream.iterate(0, i -> i + 1).limit(numElements);
Configuration config = new Configuration().from(new StreamProducer<>(inputElements)).to(new Counter<>()).end(new CollectorSink<>(processedElements));
TeeTimeScheduler scheduling = new GlobalTaskPoolScheduling(numThreads, config, numExecutions);
Execution<Configuration> execution = new Execution<>(config, true, scheduling);
execution.executeBlocking();
for (int i = 0; i < numElements; i++) {
Integer actualElement = processedElements.get(i);
assertThat(actualElement, is(i));
}
assertThat(processedElements, hasSize(numElements));
}
use of java.util.stream.IntStream in project jdk8u_jdk by JetBrains.
the class StreamSpliteratorTest method testIntSplitting.
//
public void testIntSplitting() {
List<Consumer<IntStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
}), s -> s.reduce(Integer::sum));
List<UnaryOperator<IntStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
s -> s.map(i -> i).parallel());
for (int i = 0; i < terminalOps.size(); i++) {
setContext("termOpIndex", i);
Consumer<IntStream> terminalOp = terminalOps.get(i);
for (int j = 0; j < intermediateOps.size(); j++) {
setContext("intOpIndex", j);
UnaryOperator<IntStream> intermediateOp = intermediateOps.get(j);
for (boolean proxyEstimateSize : new boolean[] { false, true }) {
setContext("proxyEstimateSize", proxyEstimateSize);
// Size is assumed to be larger than the target size for no splitting
// @@@ Need way to obtain the target size
Spliterator.OfInt sp = intermediateOp.apply(IntStream.range(0, 1000)).spliterator();
ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
IntStream s = StreamSupport.intStream(psp, true);
terminalOp.accept(s);
Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
}
}
}
}
Aggregations