java.util.stream.IntStream.generate()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(145)

本文整理了Java中java.util.stream.IntStream.generate()方法的一些代码示例,展示了IntStream.generate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IntStream.generate()方法的具体详情如下:
包路径:java.util.stream.IntStream
类名称:IntStream
方法名:generate

IntStream.generate介绍

暂无

代码示例

代码示例来源:origin: apache/geode

public synchronized int getAvailablePort() {
  int result = IntStream.generate(supplier)
    .filter(port -> !usedPorts.contains(port))
    .findFirst()
    .getAsInt();

  usedPorts.add(result);
  return result;
 }
}

代码示例来源:origin: speedment/speedment

static IntStream repeat(int item, int times) {
  return IntStream.generate(() -> item).limit(times);
}

代码示例来源:origin: biezhi/30-seconds-of-java8

public static int[] initializeArrayWithValues(int n, int value) {
  return IntStream.generate(() -> value).limit(n).toArray();
}

代码示例来源:origin: prestodb/presto

case ".*x.*":
  pattern = Slices.utf8Slice(".*x.*");
  IntStream.generate(() -> 97).limit(sourceLength).forEach(sliceOutput::appendByte);
  break;
case ".*(x|y).*":
  pattern = Slices.utf8Slice(".*(x|y).*");
  IntStream.generate(() -> 97).limit(sourceLength).forEach(sliceOutput::appendByte);
  break;
case "longdotstar":

代码示例来源:origin: shekhargulati/30-seconds-of-java

public static int[] initializeArrayWithValues(int n, int value) {
  return IntStream.generate(() -> value).limit(n).toArray();
}

代码示例来源:origin: one.util/streamex

/**
 * Returns an infinite sequential unordered stream where each element is
 * generated by the provided {@code IntSupplier}. This is suitable for
 * generating constant streams, streams of random elements, etc.
 *
 * @param s the {@code IntSupplier} for generated elements
 * @return a new infinite sequential unordered {@code IntStreamEx}
 * @see IntStream#generate(IntSupplier)
 */
public static IntStreamEx generate(IntSupplier s) {
  return seq(IntStream.generate(s));
}

代码示例来源:origin: com.speedment.runtime/runtime-core

static IntStream repeat(int item, int times) {
  return IntStream.generate(() -> item).limit(times);
}

代码示例来源:origin: hibernate/hibernate-search

public Stream<AbstractBookEntity> stream() {
  return IntStream.generate( this::next ).limit( streamedAddsPerFlush )
      .mapToObj( dataset::create );
}

代码示例来源:origin: ch.netzwerg/chabis

public static List<String> randomWords(Random random, int count) {
  IntStream randomIndices = IntStream.generate(() -> randomIndex(random)).limit(count);
  return randomIndices.mapToObj(ALL_WORDS::get).collect(toList());
}

代码示例来源:origin: dunwu/javacore

public static void main(String[] args) {
    SecureRandom secureRandom = new SecureRandom(new byte[]{1, 3, 3, 7});
    int[] randoms = IntStream.generate(secureRandom::nextInt)
      .filter(n -> n > 0)
      .limit(10)
      .toArray();
    System.out.println(Arrays.toString(randoms));

    int[] nums = IntStream.iterate(1, n -> n * 2)
      .limit(11)
      .toArray();
    System.out.println(Arrays.toString(nums));
  }
}

代码示例来源:origin: hazelcast/hazelcast-jet

private TestOutbox createOutbox() {
  return new TestOutbox(IntStream.generate(() -> 1).limit(expectedOutputs.size()).toArray(), 1);
}

代码示例来源:origin: apache/jena

/**
 * Generate a random sequence between low (inclusive) and high (exclusive) - with duplicates or not
 */
public static int[] rand(int numRand, int low, int high, boolean allDifferent) {
  Set<Integer> used = new HashSet<>();
  IntSupplier supplier = allDifferent ? () -> {
    int x = oneRandomInt(low, high);
    while (!used.add(x)) x = oneRandomInt(low, high);
    return x;
  } : () -> oneRandomInt(low, high);
  return IntStream.generate(supplier).limit(numRand).toArray();
}

代码示例来源:origin: com.simiacryptus/mindseye

/**
 * Refresh sampled data.
 */
protected void refreshSampledData() {
 assert 0 < trainingData.size();
 Tensor[][] trainingData;
 if (0 < getTrainingSize() && getTrainingSize() < this.trainingData.size() - 1) {
  @Nonnull final Random random = new Random(seed);
  trainingData = IntStream.generate(() -> random.nextInt(this.trainingData.size()))
    .distinct()
    .mapToObj(i -> this.trainingData.get(i))
    .filter(x -> x != null && x.get() != null)
    .limit(getTrainingSize()).map(x -> x.get())
    .toArray(i -> new Tensor[i][]);
 } else {
  trainingData = this.trainingData.stream()
    .filter(x -> x != null && x.get() != null)
    .limit(getTrainingSize()).map(x -> x.get())
    .toArray(i -> new Tensor[i][]);
 }
 getInner().setTrainingData(trainingData);
}

代码示例来源:origin: SmartDataAnalytics/jena-sparql-api

public static SparqlViewMatcherOp<Integer> create() {
//        Function<Op, Set<Set<String>>> itemFeatureExtractor = (oop) ->
//            Collections.singleton(OpVisitorFeatureExtractor.getFeatures(oop, (op) -> op.getClass().getSimpleName()));

    Iterator<Integer> nextPatternIdIt =
        IntStream.generate(new AtomicInteger()::getAndIncrement).iterator();

    Supplier<Integer> supplier = () -> nextPatternIdIt.next();

    SparqlViewMatcherOp<Integer> result = new SparqlViewMatcherOpImpl<>(
        op -> QueryToGraph.normalizeOp(op, false),
        SparqlViewMatcherOpImpl::extractFeatures,
        new OpIndexerImpl(),
        supplier);

    return result;
  }

代码示例来源:origin: eventsourcing/es4j

@DataProvider(name = "delays", parallel = true)
public static Iterator<Object[]> delays() {
  return IntStream.generate(() -> new Random().nextInt(3000)).
      limit(ForkJoinPool.getCommonPoolParallelism() * 10).
              boxed().
              map(i -> new Object[]{i}).
              collect(Collectors.toList()).
              iterator();
}

代码示例来源:origin: org.infinispan/infinispan-query

private String getStringKeyForCache(Cache cache) {
 LocalizedCacheTopology topology = cache.getAdvancedCache().getDistributionManager().getCacheTopology();
 return IntStream.generate(ThreadLocalRandom.current()::nextInt).mapToObj(i -> "key" + i)
    .filter(key -> topology.getDistribution(key).isPrimary()).findAny().get();
}

代码示例来源:origin: FoundationDB/fdb-record-layer

@Test
@Tag(Tags.Slow)
public void addSequentialWhileBuildingVersion() {
  Random r = new Random(0x8badf00d);
  List<TestRecords1Proto.MySimpleRecord> records = LongStream.range(0, 100).mapToObj( val ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(val).setNumValue2(r.nextInt(20)).build()
  ).collect(Collectors.toList());
  Set<Integer> usedKeys = new HashSet<>();
  List<Integer> primaryKeys = IntStream.generate(() -> r.nextInt(100)).filter(usedKeys::add).limit(50).boxed().collect(Collectors.toList());
  List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding = primaryKeys.stream().map(recNo ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(recNo).setNumValue2(r.nextInt(20) + 20).build()
  ).collect(Collectors.toList());
  versionRebuild(records, recordsWhileBuilding);
}

代码示例来源:origin: FoundationDB/fdb-record-layer

@Test
@Tag(Tags.Slow)
public void addSequentialWhileBuildingParallelVersion() {
  Random r = new Random(0x8badf00d);
  List<TestRecords1Proto.MySimpleRecord> records = LongStream.range(0, 100).mapToObj( val ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(val).setNumValue2(r.nextInt(20)).build()
  ).collect(Collectors.toList());
  Set<Integer> usedKeys = new HashSet<>();
  List<Integer> primaryKeys = IntStream.generate(() -> r.nextInt(100)).filter(usedKeys::add).limit(50).boxed().collect(Collectors.toList());
  List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding = primaryKeys.stream().map(recNo ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(recNo).setNumValue2(r.nextInt(20) + 20).build()
  ).collect(Collectors.toList());
  versionRebuild(records, recordsWhileBuilding, 5, false);
}

代码示例来源:origin: FoundationDB/fdb-record-layer

@Test
@Tag(Tags.Slow)
public void sequentialWhileBuildingWithoutVersion() {
  Random r = new Random(0x8badf00d);
  List<TestRecords1Proto.MySimpleRecord> records = LongStream.range(0, 100).mapToObj(val ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(val).setNumValue2(r.nextInt(20)).build()
  ).limit(100).collect(Collectors.toList());
  openSimpleMetaData(metaDataBuilder -> metaDataBuilder.setStoreRecordVersions(false));
  Set<Integer> usedKeys = new HashSet<>();
  List<Integer> primaryKeys = IntStream.generate(() -> r.nextInt(100)).filter(usedKeys::add).limit(50).boxed().collect(Collectors.toList());
  List<TestRecords1Proto.MySimpleRecord> recordsWhileBuilding = primaryKeys.stream().map(recNo ->
      TestRecords1Proto.MySimpleRecord.newBuilder().setRecNo(recNo).setNumValue2(r.nextInt(20) + 20).build()
  ).collect(Collectors.toList());
  try (FDBRecordContext context = openContext(false)) {
    records.stream().filter(msg -> msg.getNumValue2() % 2 == 0).forEach(recordStore::saveRecord);
    context.commit();
  }
  versionRebuild(records, recordsWhileBuilding, 1, false);
}

代码示例来源:origin: apache/sis

/**
   * Creates a new list whose capacity and value magnitude are defined as input.
   * The list is filled by a random integer generator before return.
   *
   * @param  size      number of elements to insert in the list.
   * @param  maxValue  maximum value to use for value insertion.
   * @return a fresh and filled list.
   */
  private static IntegerList createRandomlyFilled(final int size, final int maxValue) {
    final Random random = TestUtilities.createRandomNumberGenerator();
    return IntStream.generate(() -> random.nextInt(maxValue))
        .limit(size)
        .collect(() -> new IntegerList(size, maxValue), IntegerList::addInt, (l1, l2) -> l1.addAll(l2));
  }
}

相关文章

微信公众号

最新文章

更多