Search in sources :

Example 1 with IntStream

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);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) IntStream(java.util.stream.IntStream)

Example 2 with IntStream

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);
        }
    }
}
Also used : Random(java.util.Random) BitSet(java.util.BitSet) IntStream(java.util.stream.IntStream) Test(org.testng.annotations.Test)

Example 3 with IntStream

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));
}
Also used : IntStream(java.util.stream.IntStream) Configuration(teetime.framework.Configuration) StreamProducer(teetime.stage.StreamProducer) List(java.util.List) CollectorSink(teetime.stage.CollectorSink) Execution(teetime.framework.Execution) GlobalTaskPoolScheduling(teetime.framework.scheduling.globaltaskpool.GlobalTaskPoolScheduling) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) TeeTimeScheduler(teetime.framework.TeeTimeScheduler) Assert(org.junit.Assert) ArrayList(java.util.ArrayList) Configuration(teetime.framework.Configuration) ArrayList(java.util.ArrayList) StreamProducer(teetime.stage.StreamProducer) Execution(teetime.framework.Execution) GlobalTaskPoolScheduling(teetime.framework.scheduling.globaltaskpool.GlobalTaskPoolScheduling) TeeTimeScheduler(teetime.framework.TeeTimeScheduler) IntStream(java.util.stream.IntStream)

Example 4 with IntStream

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));
}
Also used : IntStream(java.util.stream.IntStream) MethodSorters(org.junit.runners.MethodSorters) StreamProducer(teetime.stage.StreamProducer) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) TeeTimeScheduler(teetime.framework.TeeTimeScheduler) ArrayList(java.util.ArrayList) Configuration(teetime.framework.Configuration) List(java.util.List) CollectorSink(teetime.stage.CollectorSink) Execution(teetime.framework.Execution) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Counter(teetime.stage.Counter) Configuration(teetime.framework.Configuration) ArrayList(java.util.ArrayList) Counter(teetime.stage.Counter) Execution(teetime.framework.Execution) TeeTimeScheduler(teetime.framework.TeeTimeScheduler) IntStream(java.util.stream.IntStream)

Example 5 with IntStream

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));
            }
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) SpliteratorTestHelper(java.util.stream.SpliteratorTestHelper) IntConsumer(java.util.function.IntConsumer) UnaryOperator(java.util.function.UnaryOperator) Test(org.testng.annotations.Test) DoubleConsumer(java.util.function.DoubleConsumer) Function(java.util.function.Function) Assert(org.testng.Assert) LambdaTestHelpers.dpEven(java.util.stream.LambdaTestHelpers.dpEven) LambdaTestHelpers.irDoubler(java.util.stream.LambdaTestHelpers.irDoubler) StreamSupport(java.util.stream.StreamSupport) IntStreamTestDataProvider(java.util.stream.IntStreamTestDataProvider) LambdaTestHelpers.countTo(java.util.stream.LambdaTestHelpers.countTo) LongStream(java.util.stream.LongStream) LambdaTestHelpers.lpEven(java.util.stream.LambdaTestHelpers.lpEven) DoubleStreamTestDataProvider(java.util.stream.DoubleStreamTestDataProvider) TestData(java.util.stream.TestData) StreamTestDataProvider(java.util.stream.StreamTestDataProvider) LambdaTestHelpers.permuteStreamFunctions(java.util.stream.LambdaTestHelpers.permuteStreamFunctions) LongConsumer(java.util.function.LongConsumer) DoubleStream(java.util.stream.DoubleStream) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) LambdaTestHelpers(java.util.stream.LambdaTestHelpers) OpTestCase(java.util.stream.OpTestCase) LambdaTestHelpers.mDoubler(java.util.stream.LambdaTestHelpers.mDoubler) LongStreamTestDataProvider(java.util.stream.LongStreamTestDataProvider) LambdaTestHelpers.pEven(java.util.stream.LambdaTestHelpers.pEven) LambdaTestHelpers.ipEven(java.util.stream.LambdaTestHelpers.ipEven) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) UnaryOperator(java.util.function.UnaryOperator) IntStream(java.util.stream.IntStream) Spliterator(java.util.Spliterator)

Aggregations

IntStream (java.util.stream.IntStream)88 Test (org.junit.Test)20 List (java.util.List)16 ArrayList (java.util.ArrayList)13 Arrays (java.util.Arrays)11 Stream (java.util.stream.Stream)10 Test (org.junit.jupiter.api.Test)8 Random (java.util.Random)7 Collectors (java.util.stream.Collectors)7 Map (java.util.Map)6 DoubleStream (java.util.stream.DoubleStream)6 LongStream (java.util.stream.LongStream)6 Pattern (java.util.regex.Pattern)5 DecimalBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.decimalBox.definition.DecimalBoxFieldDefinition)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)5 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)5 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)5 LayoutRow (org.uberfire.ext.layout.editor.api.editor.LayoutRow)5 LayoutTemplate (org.uberfire.ext.layout.editor.api.editor.LayoutTemplate)5 Function (java.util.function.Function)4