java.util.stream.IntStream#forEach ( )源码实例Demo

下面列出了java.util.stream.IntStream#forEach ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: presto   文件: TestTypedKeyValueHeap.java
private static void test(IntStream keyInputStream, Stream<String> valueInputStream, BlockComparator comparator, Iterator<String> outputIterator)
{
    BlockBuilder keysBlockBuilder = BIGINT.createBlockBuilder(null, INPUT_SIZE);
    BlockBuilder valuesBlockBuilder = VARCHAR.createBlockBuilder(null, INPUT_SIZE);
    keyInputStream.forEach(x -> BIGINT.writeLong(keysBlockBuilder, x));
    valueInputStream.forEach(x -> VARCHAR.writeString(valuesBlockBuilder, x));

    TypedKeyValueHeap heap = new TypedKeyValueHeap(comparator, BIGINT, VARCHAR, OUTPUT_SIZE);
    heap.addAll(keysBlockBuilder, valuesBlockBuilder);

    BlockBuilder resultBlockBuilder = VARCHAR.createBlockBuilder(null, OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);

    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(VARCHAR.getSlice(resultBlock, i).toStringUtf8(), outputIterator.next());
    }
}
 
源代码2 项目: presto   文件: TestTypedHeap.java
private static void test(IntStream inputStream, BlockComparator comparator, PrimitiveIterator.OfInt outputIterator)
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, INPUT_SIZE);
    inputStream.forEach(x -> BIGINT.writeLong(blockBuilder, x));

    TypedHeap heap = new TypedHeap(comparator, BIGINT, OUTPUT_SIZE);
    heap.addAll(blockBuilder);

    BlockBuilder resultBlockBuilder = BIGINT.createBlockBuilder(null, OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);

    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(BIGINT.getLong(resultBlock, i), outputIterator.nextInt());
    }
}
 
@Test
public void testParallelPartitionDrops()
{
    int partitionsToDrop = 5;
    IntStream dropThreadsConfig = IntStream.of(1, 2);
    dropThreadsConfig.forEach(dropThreads -> {
        countDownLatch = new CountDownLatch(dropThreads);
        SemiTransactionalHiveMetastore semiTransactionalHiveMetastore = getSemiTransactionalHiveMetastoreWithDropExecutor(newFixedThreadPool(dropThreads));
        IntStream.range(0, partitionsToDrop).forEach(i -> semiTransactionalHiveMetastore.dropPartition(SESSION,
                "test",
                "test",
                ImmutableList.of(String.valueOf(i)),
                true));
        semiTransactionalHiveMetastore.commit();
    });
}
 
源代码4 项目: pyramid   文件: LogisticLoss.java
private void updatePredictedCounts(){
    StopWatch stopWatch = new StopWatch();
    if (logger.isDebugEnabled()){
        stopWatch.start();
    }
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0,numParameters).parallel();
    } else {
        intStream = IntStream.range(0,numParameters);
    }

    intStream.forEach(i -> this.predictedCounts.set(i, calPredictedCount(i)));
    if (logger.isDebugEnabled()){
        logger.debug("time spent on updatePredictedCounts = "+stopWatch);
    }
}
 
源代码5 项目: consulo   文件: TroveUtil.java
public static void processBatches(@Nonnull IntStream stream, int batchSize, @Nonnull Consumer<TIntHashSet> consumer) {
  Ref<TIntHashSet> batch = new Ref<>(new TIntHashSet());
  stream.forEach(commit -> {
    batch.get().add(commit);
    if (batch.get().size() >= batchSize) {
      try {
        consumer.consume(batch.get());
      }
      finally {
        batch.set(new TIntHashSet());
      }
    }
  });

  if (!batch.get().isEmpty()) {
    consumer.consume(batch.get());
  }
}
 
源代码6 项目: morpheus-core   文件: Mapper.java
/**
 * Returns a new list which is the result of applying the mapping function to values of the input list
 * @param list          the input list on which to apply the mapping function to all elements
 * @param parallel      true if parallel mapping should be used
 * @param listMapper    the list mapper function to apply to all values in input list
 * @param <I>           the input list type
 * @param <O>           the output list type
 * @return              the output list
 */
public static <I,O> List<O> apply(List<I> list, boolean parallel, ListMapper<I,O> listMapper) {
    final int size = list.size();
    final List<O> result = createList(list);
    IntStream.range(0, size).forEach(i -> result.add(null));
    final IntStream indexes = parallel ? IntStream.range(0, size).parallel() : IntStream.range(0, size);
    indexes.forEach(index -> {
        final I source = list.get(index);
        final O target = listMapper.apply(index, source);
        result.set(index, target);
    });
    return result;
}
 
源代码7 项目: registry   文件: SlotSynchronizerTest.java
@Test
public void testCountSlots() throws Exception {
    SlotSynchronizer<Integer> slotSynchronizer = new SlotSynchronizer<>();
    int count = new Random().nextInt(1000) + 1;

    Collection<SlotSynchronizer<Integer>.Lock> locks = new ArrayList<>();
    IntStream keysStream = IntStream.range(0, count);
    keysStream.forEach(value -> locks.add(slotSynchronizer.lockSlot(value)));

    Assert.assertEquals(count, slotSynchronizer.occupiedSlots());

    locks.forEach(lock -> lock.unlock());
    Assert.assertEquals(0, slotSynchronizer.occupiedSlots());

}
 
源代码8 项目: RankSys   文件: AbstractFastRecommender.java
@Override
public FastRecommendation getRecommendation(int uidx, IntStream candidates) {
    IntSet set = new IntOpenHashSet();
    candidates.forEach(set::add);

    return getRecommendation(uidx, set::contains);
}
 
源代码9 项目: chuidiang-ejemplos   文件: Java8RandomExample.java
public static void main(String[] args) {
    Random random = new Random();

    IntStream intStream = random.ints(10, 1, 7);
    Iterator iterator = intStream.iterator();

    while (iterator.hasNext()){
        System.out.println("Random Number "+iterator.next());
    }

    intStream = random.ints(10, 1, 7);
    intStream.forEach(value ->
            System.out.println("Random Number "+value)
    );
}
 
源代码10 项目: pyramid   文件: CRFLoss.java
private void updateEmpiricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
 
源代码11 项目: pyramid   文件: CRFF1Loss.java
private void updateEmpiricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
 
源代码12 项目: pyramid   文件: BlockwiseCD.java
private void updateEmpiricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
 
源代码13 项目: pyramid   文件: KLLoss.java
private void initEmpiricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
 
源代码14 项目: pyramid   文件: CMLCRFElasticNet.java
private void updateEmpiricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(this::calEmpiricalCount);
}
 
源代码15 项目: pyramid   文件: LogisticLoss.java
private void updateEmpricalCounts(){
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0, numParameters).parallel();
    } else {
        intStream = IntStream.range(0, numParameters);
    }
    intStream.forEach(i -> this.empiricalCounts.set(i, calEmpricalCount(i)));
}
 
源代码16 项目: pyramid   文件: LogisticLoss.java
private void updateClassProbMatrix(){
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    IntStream intStream;
    if (isParallel){
        intStream = IntStream.range(0,dataSet.getNumDataPoints()).parallel();
    } else {
        intStream = IntStream.range(0,dataSet.getNumDataPoints());
    }
    intStream.forEach(this::updateClassProbs);
    this.isProbabilityCacheValid = true;
    if (logger.isDebugEnabled()){
        logger.debug("time spent on updateClassProbMatrix = "+stopWatch);
    }
}
 
源代码17 项目: morpheus-core   文件: Bounds.java
/**
 * Retruns the integer bounds of a stream of ints
 * @param stream    the stream to compute bounds on
 * @return          the bounds for stream, empty if no data in stream
 */
public static Optional<Bounds<Integer>> ofInts(IntStream stream) {
    final OfInts calculator = new OfInts();
    stream.forEach(calculator::add);
    return calculator.getBounds();
}
 
源代码18 项目: tablesaw   文件: IntColumn.java
public static IntColumn create(String name, IntStream stream) {
  IntArrayList list = new IntArrayList();
  stream.forEach(list::add);
  return new IntColumn(name, list);
}
 
源代码19 项目: tablesaw   文件: IntColumn.java
public static IntColumn create(String name, IntStream stream) {
  IntArrayList list = new IntArrayList();
  stream.forEach(list::add);
  return new IntColumn(name, list);
}
 
源代码20 项目: flink   文件: SourceOperatorStreamTaskTest.java
private TaskStateSnapshot executeAndWaitForCheckpoint(
		long checkpointId,
		TaskStateSnapshot initialSnapshot,
		IntStream expectedRecords) throws Exception {

	try (StreamTaskMailboxTestHarness<Integer> testHarness = createTestHarness(checkpointId, initialSnapshot)) {
		// Add records to the splits.
		MockSourceSplit split = getAndMaybeAssignSplit(testHarness);
		// Add records to the split and update expected output.
		addRecords(split, NUM_RECORDS);
		// Process all the records.
		processUntil(testHarness, () -> !testHarness.getStreamTask().inputProcessor.getAvailableFuture().isDone());

		// Trigger a checkpoint.
		CheckpointOptions checkpointOptions = CheckpointOptions.forCheckpointWithDefaultLocation();
		OneShotLatch waitForAcknowledgeLatch = new OneShotLatch();
		testHarness.taskStateManager.setWaitForReportLatch(waitForAcknowledgeLatch);
		CheckpointMetaData checkpointMetaData = new CheckpointMetaData(checkpointId, checkpointId);
		Future<Boolean> checkpointFuture =
				testHarness
						.getStreamTask()
						.triggerCheckpointAsync(checkpointMetaData, checkpointOptions, false);

		// Wait until the checkpoint finishes.
		// We have to mark the source reader as available here, otherwise the runMailboxStep() call after
		// checkpiont is completed will block.
		getSourceReaderFromTask(testHarness).markAvailable();
		processUntil(testHarness, checkpointFuture::isDone);
		waitForAcknowledgeLatch.await();

		// Build expected output to verify the results
		Queue<Object> expectedOutput = new LinkedList<>();
		expectedRecords.forEach(r -> expectedOutput.offer(new StreamRecord<>(r, TimestampAssigner.NO_TIMESTAMP)));
		// Add barrier to the expected output.
		expectedOutput.add(new CheckpointBarrier(checkpointId, checkpointId, checkpointOptions));

		assertEquals(checkpointId, testHarness.taskStateManager.getReportedCheckpointId());
		assertOutputEquals("Output was not correct.", expectedOutput, testHarness.getOutput());

		return testHarness.taskStateManager.getLastJobManagerTaskStateSnapshot();
	}
}