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

x33g5p2x  于2022-01-16 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(212)

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

Stream.flatMapToInt介绍

[英]Returns an IntStream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is java.util.stream.BaseStream#close() after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)

This is an intermediate operation.
[中]返回一个IntStream,该IntStream由将提供的映射函数应用于每个元素而生成的映射流的内容替换该流的每个元素的结果组成。每个映射流都是java。util。流动BaseStream的内容放入此流后关闭()。(如果映射流为空,则使用空流。)
这是一个intermediate operation

代码示例

代码示例来源:origin: google/guava

/**
 * Returns an {@link IntStream} containing the elements of the first stream, followed by the
 * elements of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMapToInt(stream -> stream)}, but the
 * returned stream may perform better.
 *
 * @see IntStream#concat(IntStream, IntStream)
 */
public static IntStream concat(IntStream... streams) {
 // TODO(lowasser): optimize this later
 return Stream.of(streams).flatMapToInt(stream -> stream);
}

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

/**
 * Returns an {@link IntStream} containing the elements of the first stream, followed by the
 * elements of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMapToInt(stream -> stream)}, but the
 * returned stream may perform better.
 *
 * @see IntStream#concat(IntStream, IntStream)
 */
public static IntStream concat(IntStream... streams) {
 // TODO(lowasser): optimize this later
 return Stream.of(streams).flatMapToInt(stream -> stream);
}

代码示例来源:origin: google/j2objc

/**
 * Returns an {@link IntStream} containing the elements of the first stream, followed by the
 * elements of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMapToInt(stream -> stream)}, but the
 * returned stream may perform better.
 *
 * @see IntStream#concat(IntStream, IntStream)
 */
public static IntStream concat(IntStream... streams) {
 // TODO(lowasser): optimize this later
 return Stream.of(streams).flatMapToInt(stream -> stream);
}

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

/**
 * Returns an {@link IntStream} containing the elements of the first stream, followed by the
 * elements of the second stream, and so on.
 *
 * <p>This is equivalent to {@code Stream.of(streams).flatMapToInt(stream -> stream)}, but the
 * returned stream may perform better.
 *
 * @see IntStream#concat(IntStream, IntStream)
 */
public static IntStream concat(IntStream... streams) {
 // TODO(lowasser): optimize this later
 return Stream.of(streams).flatMapToInt(stream -> stream);
}

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

public FlatMapToIntAction(Function<? super T, ? extends IntStream> mapper) {
  super(s -> s.flatMapToInt(requireNonNull(mapper)), IntStream.class, FLAT_MAP_TO);
}

代码示例来源:origin: Vedenin/useful-java-links

private static void testFlatMapToInt() {
  System.out.println();
  System.out.println("Test flat map start");
  Collection<String> collection = Arrays.asList("1,2,0", "4,5");
  // Get sum all digit from strings
  int sum = collection.stream().flatMapToInt((p) -> Arrays.asList(p.split(",")).stream().mapToInt(Integer::parseInt)).sum();
  System.out.println("sum = " + sum); // print  sum = 12
}

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

private int[] propertyKeyIds()
{
  return populations.stream().flatMapToInt( this::propertyKeyIds ).distinct().toArray();
}

代码示例来源:origin: stackoverflow.com

int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
IntStream stream = Arrays.stream(arr).flatMapToInt(x -> Arrays.stream(x));

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

private int extractMaxIndex(String key, String suffixPattern) {
  // extract index and property keys
  final String escapedKey = Pattern.quote(key);
  final Pattern pattern = Pattern.compile(escapedKey + "\\.(\\d+)" + suffixPattern);
  final IntStream indexes = properties.keySet().stream()
    .flatMapToInt(k -> {
      final Matcher matcher = pattern.matcher(k);
      if (matcher.find()) {
        return IntStream.of(Integer.valueOf(matcher.group(1)));
      }
      return IntStream.empty();
    });
  // determine max index
  return indexes.max().orElse(-1);
}

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

/**
 * Flattens an array.
 *
 * @param elements input array
 * @return flattened array
 */
public static int[] flatten(Object[] elements) {
  return Arrays.stream(elements)
      .flatMapToInt(el -> el instanceof int[]
          ? Arrays.stream((int[]) el)
          : IntStream.of((int) el)
      ).toArray();
}

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

private int[] entityTokenIds()
{
  return populations.stream().flatMapToInt( population -> Arrays.stream( population.schema().getEntityTokenIds() ) ).toArray();
}

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

@Override
public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
  requireNonNull(mapper);
  return materialize().flatMapToInt(mapper);
}

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

@Override
public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
  requireNonNull(mapper);
  if (STRICT) {
    return toStream().flatMapToInt(mapper);
  }
  return mapper.apply(element);
}

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

/**
 * Deep flattens an array.
 *
 * @param input A nested array containing integers
 * @return flattened array
 */
public static int[] deepFlatten(Object[] input) {
  return Arrays.stream(input)
      .flatMapToInt(o -> {
        if (o instanceof Object[]) {
          return Arrays.stream(deepFlatten((Object[]) o));
        }
        return IntStream.of((Integer) o);
      }).toArray();
}

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

public static IntStream rleRegion() {
 int numRuns = ThreadLocalRandom.current().nextInt(1, 2048);
 int[] runs = createSorted16BitInts(numRuns * 2);
 return IntStream.range(0, numRuns)
     .map(i -> i * 2)
     .mapToObj(i -> IntStream.range(runs[i], runs[i + 1]))
     .flatMapToInt(i -> i);
}

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

@Override
public int getNodes() {
  return IntStream.concat(
      IntStream.of(baseGraph.getNodes()-1),
      extraEdges.stream().flatMapToInt(edge -> IntStream.of(edge.getBaseNode(), edge.getAdjNode())))
      .max().getAsInt()+1;
}

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

@Override
  public IntStream build(boolean parallel) {
    return previous().build(parallel).flatMapToInt(mapper);
  }
}

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

@Override
public IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper) {
  return wrap(stream().flatMapToInt(mapper));
}

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

@Override
public int getNodes() {
  return IntStream.concat(
      IntStream.of(graphHopperStorage.getNodes()-1),
      additionalEdges.stream().flatMapToInt(edge -> IntStream.of(edge.getBaseNode(), edge.getAdjNode())))
      .max().getAsInt()+1;
}

代码示例来源:origin: SonarSource/sonarqube

/**
 * Extract the lines of all the locations in the specified component. All the flows and secondary locations
 * are taken into account. The lines present in multiple flows and locations are kept
 * duplicated. Ordering of results is not guaranteed.
 * <p>
 * TODO should be a method of DefaultIssue, as soon as it's no
 * longer in sonar-core and can access sonar-db-dao.
 */
public static IntStream allLinesFor(DefaultIssue issue, String componentId) {
 DbIssues.Locations locations = issue.getLocations();
 if (locations == null) {
  return IntStream.empty();
 }
 Stream<DbCommons.TextRange> textRanges = Stream.concat(
  locations.hasTextRange() ? Stream.of(locations.getTextRange()) : Stream.empty(),
  locations.getFlowList().stream()
   .flatMap(f -> f.getLocationList().stream())
   .filter(l -> Objects.equals(componentIdOf(issue, l), componentId))
   .map(DbIssues.Location::getTextRange));
 return textRanges.flatMapToInt(range -> IntStream.rangeClosed(range.getStartLine(), range.getEndLine()));
}

相关文章