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

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

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

Stream.flatMap介绍

[英]Returns a stream 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 nullan empty stream is used, instead.)

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

代码示例

代码示例来源:origin: spring-projects/spring-framework

private List<MediaType> getMediaTypes(List<View> views) {
  return views.stream()
      .flatMap(view -> view.getSupportedMediaTypes().stream())
      .collect(Collectors.toList());
}

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

public static <K, V> Map<K, V> mergeMaps(Stream<Map<K, V>> mapStream, BinaryOperator<V> merger)
  {
    return mapStream
        .map(Map::entrySet)
        .flatMap(Collection::stream)
        .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, merger));
  }
}

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

private static Stream<Method> methodsStream( List<Class<?>> interfaces )
{
  return interfaces.stream().map( Class::getMethods ).flatMap( Arrays::stream );
}

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

@Override
public List<Symbol> getOutputSymbols()
{
  return ImmutableList.<Symbol>builder()
      .addAll(groupingSets.stream()
          .flatMap(Collection::stream)
          .collect(toSet()))
      .addAll(aggregationArguments)
      .add(groupIdSymbol)
      .build();
}

代码示例来源:origin: apache/incubator-druid

Stream<SegmentWithState> getAppendingSegments(Collection<String> sequenceNames)
{
 synchronized (segments) {
  return sequenceNames
    .stream()
    .map(segments::get)
    .filter(Objects::nonNull)
    .flatMap(segmentsForSequence -> segmentsForSequence.intervalToSegmentStates.values().stream())
    .map(segmentsOfInterval -> segmentsOfInterval.appendingSegment)
    .filter(Objects::nonNull);
 }
}

代码示例来源:origin: apache/incubator-druid

public Collection<Server> getAllServers()
{
 return ((Collection<List<Server>>) hostSelector.getAllBrokers().values()).stream()
                                      .flatMap(Collection::stream)
                                      .collect(Collectors.toList());
}

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

public Set<File> idFiles()
{
  return Arrays.stream( DatabaseFile.values() )
         .filter( DatabaseFile::hasIdFile )
        .flatMap( value -> Streams.ofOptional( idFile( value ) ) )
         .collect( Collectors.toSet() );
}

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

/**
 * Gets a stream of values from a collection of range resources.
 */
public static LongStream rangeValues(Collection<Protos.Resource> resources) {
  checkNotNull(resources);
  return resources.stream()
    .filter(Protos.Resource::hasRanges)
    .flatMap(r -> r.getRanges().getRangeList().stream())
    .flatMapToLong(Utils::rangeValues);
}

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

@Override
  public void validate(PlanNode plan, Session session, Metadata metadata, SqlParser sqlParser, TypeProvider types, WarningCollector warningCollector)
  {
    searchFrom(plan)
        .where(AggregationNode.class::isInstance)
        .<AggregationNode>findAll()
        .stream()
        .flatMap(node -> node.getAggregations().values().stream())
        .filter(aggregation -> aggregation.getCall().getFilter().isPresent())
        .forEach(ignored -> {
          throw new IllegalStateException("Generated plan contains unimplemented filtered aggregations");
        });
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void applyCookies() {
  getCookies().values().stream().flatMap(Collection::stream)
      .map(cookie -> new DefaultCookie(cookie.getName(), cookie.getValue()))
      .forEach(this.request::addCookie);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a new {@link MockMultipartHttpServletRequest} based on the
 * supplied {@code ServletContext} and the {@code MockMultipartFiles}
 * added to this builder.
 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
  MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
  this.files.stream().forEach(request::addFile);
  this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);
  if (!this.parts.isEmpty()) {
    new StandardMultipartHttpServletRequest(request)
        .getMultiFileMap().values().stream().flatMap(Collection::stream)
        .forEach(request::addFile);
  }
  return request;
}

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

private Stream<CompilationMessage> findDuplicates( Collection<Element> visitedProcedures )
{
  return indexByName( visitedProcedures ).filter( index -> index.getValue().size() > 1 )
      .flatMap( this::asErrors );
}

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

@Override
public List<Expression> getExpressions()
{
  return sets.stream()
      .flatMap(List::stream)
      .collect(Collectors.toList());
}

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

public static List<? extends SqlFunction> extractFunctions(Collection<Class<?>> classes)
{
  return classes.stream()
      .map(FunctionExtractor::extractFunctions)
      .flatMap(Collection::stream)
      .collect(toImmutableList());
}

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

public MultiJoinNode(LinkedHashSet<PlanNode> sources, Expression filter, List<Symbol> outputSymbols)
{
  requireNonNull(sources, "sources is null");
  checkArgument(sources.size() > 1, "sources size is <= 1");
  requireNonNull(filter, "filter is null");
  requireNonNull(outputSymbols, "outputSymbols is null");
  this.sources = sources;
  this.filter = filter;
  this.outputSymbols = ImmutableList.copyOf(outputSymbols);
  List<Symbol> inputSymbols = sources.stream().flatMap(source -> source.getOutputSymbols().stream()).collect(toImmutableList());
  checkArgument(inputSymbols.containsAll(outputSymbols), "inputs do not contain all output symbols");
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Returns initial {@link Method} as well as all matching ones found in interfaces.
 * This allows to introspect metadata for a method which is both declared in parent class and in implemented
 * interface(s). <code>interfaces</code> typically is obtained by {@link ClassUtils#getAllInterfacesAsSet}
 */
Collection<Method> getMethodAndInterfaceDeclarations(Method method, Collection<Class> interfaces) {
  final List<Method> methods = new ArrayList<>();
  methods.add(method);
  // we search for matching method by iteration and comparison vs getMethod to avoid repeated NoSuchMethodException
  // being thrown, while interface typically only define a few set of methods to check.
  interfaces.stream()
      .map(Class::getMethods)
      .flatMap(Arrays::stream)
      .filter(m -> m.getName().equals(method.getName()) && Arrays.equals(m.getParameterTypes(), method.getParameterTypes()))
      .findFirst()
      .ifPresent(methods::add);
  return methods;
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
public void upgrade() {
  this.indexSetService.findAll()
    .stream()
    .map(mongoIndexSetFactory::create)
    .flatMap(indexSet -> getReopenedIndices(indexSet).stream())
    .map(indexName -> { LOG.debug("Marking index {} to be reopened using alias.", indexName); return indexName; })
    .forEach(indices::markIndexReopened);
}

代码示例来源:origin: apache/incubator-druid

private List<DataSegment> getAvailableDataSegments()
{
 return metadataSegmentManager.getDataSources()
                .stream()
                .flatMap(source -> source.getSegments().stream())
                .collect(Collectors.toList());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void applyCookies() {
  getCookies().values().stream().flatMap(Collection::stream)
      .map(cookie -> new HttpCookie(cookie.getName(), cookie.getValue()))
      .forEach(this.jettyRequest::cookie);
}

代码示例来源:origin: eclipse-vertx/vert.x

public synchronized List<T> handlers() {
 return handlerMap.values().stream()
  .flatMap(handlers -> handlers.list.stream())
  .map(holder -> holder.handler)
  .collect(Collectors.toList());
}

相关文章