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

x33g5p2x  于2022-01-17 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(242)

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

Collectors.toMap介绍

暂无

代码示例

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

private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection<Part>> map) {
  return new LinkedMultiValueMap<>(map.entrySet().stream()
      .collect(Collectors.toMap(Map.Entry::getKey, e -> toList(e.getValue()))));
}

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

public static <K, V> Map<K, V> toMap(List<JsonSerializableEntry<K, V>> list)
  {
    return list.stream()
        .collect(Collectors.toMap(JsonSerializableEntry::getKey, JsonSerializableEntry::getValue));
  }
}

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

protected final Map<String, Object> getSessionAttributes(MockHttpServletRequest request) {
  HttpSession session = request.getSession(false);
  if (session != null) {
    Enumeration<String> attrNames = session.getAttributeNames();
    if (attrNames != null) {
      return Collections.list(attrNames).stream().
          collect(Collectors.toMap(n -> n, session::getAttribute));
    }
  }
  return Collections.emptyMap();
}

代码示例来源:origin: hs-web/hsweb-framework

private static Map<String, ClassProperty> createProperty(Class type) {
  List<String> fieldNames = Arrays.stream(type.getDeclaredFields())
      .map(Field::getName).collect(Collectors.toList());
  return Stream.of(propertyUtils.getPropertyDescriptors(type))
      .filter(property -> !property.getName().equals("class") && property.getReadMethod() != null && property.getWriteMethod() != null)
      .map(BeanClassProperty::new)
      //让字段有序
      .sorted(Comparator.comparing(property -> fieldNames.indexOf(property.name)))
      .collect(Collectors.toMap(ClassProperty::getName, Function.identity(), (k, k2) -> k, LinkedHashMap::new));
}

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

/**
 * Returns a map of dataSource to the total byte size of segments managed by this segmentManager.  This method should
 * be used carefully because the returned map might be different from the actual data source states.
 *
 * @return a map of dataSources and their total byte sizes
 */
public Map<String, Long> getDataSourceSizes()
{
 return dataSources.entrySet().stream()
          .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getTotalSegmentSize()));
}

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

/**
 * Given two maps (A, B), returns all the key-value pairs in A whose keys are not contained in B
 */
public static <K, V> Map<K, V> subtractMap(Map<? extends K, ? extends V> minuend, Map<? extends K, ? extends V> subtrahend) {
  return minuend.entrySet().stream()
      .filter(entry -> !subtrahend.containsKey(entry.getKey()))
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

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

protected BoltRequestMessageReader( BoltConnection connection, BoltResponseHandler externalErrorResponseHandler,
    List<RequestMessageDecoder> decoders )
{
  this.connection = connection;
  this.externalErrorResponseHandler = externalErrorResponseHandler;
  this.decoders = decoders.stream().collect( toMap( RequestMessageDecoder::signature, identity() ) );
}

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

/**
 * Creates a map of 'field name' to 'field value' from all {@code public} {@code int} fields
 * of a class.
 * @param cls source class
 * @return unmodifiable name to value map
 */
public static Map<String, Integer> nameToValueMapFromPublicIntFields(Class<?> cls) {
  final Map<String, Integer> map = Arrays.stream(cls.getDeclaredFields())
    .filter(fld -> Modifier.isPublic(fld.getModifiers()) && fld.getType() == Integer.TYPE)
    .collect(Collectors.toMap(Field::getName, fld -> getIntFromField(fld, fld.getName())));
  return Collections.unmodifiableMap(map);
}

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

/**
 * Returns a map of dataSource to the number of segments managed by this segmentManager.  This method should be
 * carefully because the returned map might be different from the actual data source states.
 *
 * @return a map of dataSources and number of segments
 */
public Map<String, Long> getDataSourceCounts()
{
 return dataSources.entrySet().stream()
          .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getNumSegments()));
}

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

private Map<String, Long> getDeltaValues(Map<String, Long> total, Map<String, Long> prev)
{
 return total.entrySet()
       .stream()
       .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue() - prev.getOrDefault(e.getKey(), 0L)));
}

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

AtopTable(String name, String atopLabel, List<AtopColumn> columns)
{
  this.atopLabel = atopLabel;
  this.name = requireNonNull(name, "name is null");
  this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
  columnIndex = this.columns.stream().collect(Collectors.toMap(AtopColumn::getName, Function.identity()));
}

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

@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
  return tables.values().stream()
      .filter(table -> prefix.matches(table.toSchemaTableName()))
      .collect(toMap(BlackHoleTableHandle::toSchemaTableName, handle -> handle.toTableMetadata().getColumns()));
}

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

static Map<String, List<String>> allImports() {
  return TestImportGroup.imports.entrySet()
      .stream()
      .collect(Collectors.toMap((entry) -> entry.getKey().getClassName(),
          Map.Entry::getValue));
}
private final List<Entry> instanceImports = new ArrayList<>();

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

private Mono<Map<String, Object>> initAttributes(ServerWebExchange exchange) {
  if (this.sessionAttributePredicate == null) {
    return EMPTY_ATTRIBUTES;
  }
  return exchange.getSession().map(session ->
      session.getAttributes().entrySet().stream()
          .filter(entry -> this.sessionAttributePredicate.test(entry.getKey()))
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

代码示例来源:origin: hs-web/hsweb-framework

public void setServers(List<OAuth2ServerConfig> servers) {
  this.servers = servers;
  repo = servers.stream()
      .collect(Collectors.toMap(OAuth2ServerConfig::getId, Function.identity()));
}

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

@Override
public synchronized Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
  return tables.values().stream()
      .filter(table -> prefix.matches(table.toSchemaTableName()))
      .collect(toMap(MemoryTableHandle::toSchemaTableName, handle -> handle.toTableMetadata().getColumns()));
}

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

public PollablePartitionsInfo(Set<TopicPartition> pollablePartitions, Map<TopicPartition, Long> earliestRetriableOffsets) {
  this.pollablePartitions = pollablePartitions;
  this.pollableEarliestRetriableOffsets = earliestRetriableOffsets.entrySet().stream()
    .filter(entry -> pollablePartitions.contains(entry.getKey()))
    .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));
}

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

private Map<String, ProcessExtensionModel> convertToMap(List<ProcessExtensionModel> processExtensionModelList){
    return processExtensionModelList.stream()
        .collect(Collectors.toMap(ProcessExtensionModel::getId,
            Function.identity()));
  }
}

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

@SuppressWarnings("unused")
@JsonProperty(FIELD_CHANGED_FIELDS)
public Map<String, Object> changedFields() {
  return Sets.intersection(originalMessage().keySet(), decoratedMessage().keySet())
    .stream()
    .filter(key -> !originalMessage().get(key).equals(decoratedMessage().get(key)))
    .collect(Collectors.toMap(Function.identity(), key -> originalMessage().get(key)));
}

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

private Map<String, String> filterMdc(Map<String, String> mdcPropertyMap) {
  if (includesMdcKeys.isEmpty()) {
    return mdcPropertyMap;
  }
  return mdcPropertyMap.entrySet()
    .stream()
    .filter(e -> includesMdcKeys.contains(e.getKey()))
    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

相关文章