java.util.Map.forEach()方法的使用及代码示例

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

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

Map.forEach介绍

暂无

代码示例

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

@Override
public Map<String, String> toSingleValueMap() {
  LinkedHashMap<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
  this.headers.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
  return singleValueMap;
}

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

/**
 * Specify mappings from type ids to Java classes, if desired.
 * This allows for synthetic ids in the type id message property,
 * instead of transferring Java class names.
 * <p>Default is no custom mappings, i.e. transferring raw Java class names.
 * @param typeIdMappings a Map with type id values as keys and Java classes as values
 */
public void setTypeIdMappings(Map<String, Class<?>> typeIdMappings) {
  this.idClassMappings = new HashMap<>();
  typeIdMappings.forEach((id, clazz) -> {
    this.idClassMappings.put(id, clazz);
    this.classIdMappings.put(clazz, id);
  });
}

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

private StompHeaders(Map<String, List<String>> headers, boolean readOnly) {
  Assert.notNull(headers, "'headers' must not be null");
  if (readOnly) {
    Map<String, List<String>> map = new LinkedMultiValueMap<>(headers.size());
    headers.forEach((key, value) -> map.put(key, Collections.unmodifiableList(value)));
    this.headers = Collections.unmodifiableMap(map);
  }
  else {
    this.headers = headers;
  }
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
private void merge(Map<String, Object> output, Map<String, Object> map) {
  map.forEach((key, value) -> {
    Object existing = output.get(key);
    if (value instanceof Map && existing instanceof Map) {
      // Inner cast required by Eclipse IDE.
      Map<String, Object> result = new LinkedHashMap<>((Map<String, Object>) existing);
      merge(result, (Map) value);
      output.put(key, result);
    }
    else {
      output.put(key, value);
    }
  });
}

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

/**
 * Create an instance with the given map of file extensions and media types.
 */
public MappingMediaTypeFileExtensionResolver(@Nullable Map<String, MediaType> mediaTypes) {
  if (mediaTypes != null) {
    mediaTypes.forEach((extension, mediaType) -> {
      String lowerCaseExtension = extension.toLowerCase(Locale.ENGLISH);
      this.mediaTypes.put(lowerCaseExtension, mediaType);
      this.fileExtensions.add(mediaType, lowerCaseExtension);
      this.allFileExtensions.add(lowerCaseExtension);
    });
  }
}

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

private WebDataBinderFactory getDataBinderFactory(HandlerMethod handlerMethod) throws Exception {
  Class<?> handlerType = handlerMethod.getBeanType();
  Set<Method> methods = this.initBinderCache.get(handlerType);
  if (methods == null) {
    methods = MethodIntrospector.selectMethods(handlerType, INIT_BINDER_METHODS);
    this.initBinderCache.put(handlerType, methods);
  }
  List<InvocableHandlerMethod> initBinderMethods = new ArrayList<>();
  // Global methods first
  this.initBinderAdviceCache.forEach((clazz, methodSet) -> {
    if (clazz.isApplicableToBeanType(handlerType)) {
      Object bean = clazz.resolveBean();
      for (Method method : methodSet) {
        initBinderMethods.add(createInitBinderMethod(bean, method));
      }
    }
  });
  for (Method method : methods) {
    Object bean = handlerMethod.getBean();
    initBinderMethods.add(createInitBinderMethod(bean, method));
  }
  return createDataBinderFactory(initBinderMethods);
}

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

private List<Namespace> getNamespaces(Map<String, String> namespaceMappings) {
  List<Namespace> result = new ArrayList<>(namespaceMappings.size());
  namespaceMappings.forEach((prefix, namespaceUri) ->
      result.add(this.eventFactory.createNamespace(prefix, namespaceUri)));
  return result;
}

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

String parameterNameToMatch = provider.parameterNameToUse(parameterName);
    if (parameterNameToMatch != null) {
      callParameterNames.put(parameterNameToMatch.toLowerCase(), parameterName);
Map<String, Object> matchedParameters = new HashMap<>(inParameters.size());
inParameters.forEach((parameterName, parameterValue) -> {
  String parameterNameToMatch = provider.parameterNameToUse(parameterName);
  String callParameterName = callParameterNames.get(lowerCase(parameterNameToMatch));
  if (callParameterName == null) {
    if (logger.isDebugEnabled()) {
    matchedParameters.put(callParameterName, parameterValue);
if (matchedParameters.size() < callParameterNames.size()) {
  for (String parameterName : callParameterNames.keySet()) {
    String parameterNameToMatch = provider.parameterNameToUse(parameterName);
    String callParameterName = callParameterNames.get(lowerCase(parameterNameToMatch));
    if (!matchedParameters.containsKey(callParameterName) && logger.isInfoEnabled()) {
      logger.info("Unable to locate the corresponding parameter value for '" + parameterName +

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

public synchronized MemoryPoolInfo getInfo()
{
  Map<QueryId, List<MemoryAllocation>> memoryAllocations = new HashMap<>();
  for (Entry<QueryId, Map<String, Long>> entry : taggedMemoryAllocations.entrySet()) {
    List<MemoryAllocation> allocations = new ArrayList<>();
    if (entry.getValue() != null) {
      entry.getValue().forEach((tag, allocation) -> allocations.add(new MemoryAllocation(tag, allocation)));
    }
    memoryAllocations.put(entry.getKey(), allocations);
  }
  return new MemoryPoolInfo(maxBytes, reservedBytes, reservedRevocableBytes, queryMemoryReservations, memoryAllocations, queryMemoryRevocableReservations);
}

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

public List<String> getCurrentlyProcessingSegmentsAndHosts(String tier)
 {
  Map<SegmentId, String> segments = currentlyProcessingSegments.get(tier);
  List<String> retVal = new ArrayList<>();
  segments.forEach((segmentId, serverId) -> retVal.add(StringUtils.format("%s ON %s", segmentId, serverId)));
  return retVal;
 }
}

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

protected final MultiValueMap<String, String> getParamsMultiValueMap(MockHttpServletRequest request) {
  Map<String, String[]> params = request.getParameterMap();
  MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
  params.forEach((name, values) -> {
    if (params.get(name) != null) {
      for (String value : values) {
        multiValueMap.add(name, value);
      }
    }
  });
  return multiValueMap;
}

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

private static Map<String, Map<ColumnStatisticType, Block>> createColumnToComputedStatisticsMap(Map<ColumnStatisticMetadata, Block> computedStatistics)
{
  Map<String, Map<ColumnStatisticType, Block>> result = new HashMap<>();
  computedStatistics.forEach((metadata, block) -> {
    Map<ColumnStatisticType, Block> columnStatistics = result.computeIfAbsent(metadata.getColumnName(), key -> new HashMap<>());
    columnStatistics.put(metadata.getStatisticType(), block);
  });
  return result.entrySet()
      .stream()
      .collect(toImmutableMap(Entry::getKey, entry -> ImmutableMap.copyOf(entry.getValue())));
}

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

@Test
public void enrichAndValidateAttributesWithSingleElementThatOverridesAnArray() {
  Map<String, Object> attributes = new HashMap<String, Object>() {{
    // Intentionally storing 'value' as a single String instead of an array.
    // put("value", asArray("/foo"));
    put("value", "/foo");
    put("name", "test");
  }};
  Map<String, Object> expected = new HashMap<String, Object>() {{
    put("value", asArray("/foo"));
    put("path", asArray("/foo"));
    put("name", "test");
    put("method", new RequestMethod[0]);
  }};
  MapAnnotationAttributeExtractor extractor = new MapAnnotationAttributeExtractor(attributes, WebMapping.class, null);
  Map<String, Object> enriched = extractor.getSource();
  assertEquals("attribute map size", expected.size(), enriched.size());
  expected.forEach((attr, expectedValue) -> assertThat("for attribute '" + attr + "'", enriched.get(attr), is(expectedValue)));
}

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

public void forEach( BiConsumer<String,URI> consumer )
{
  entries.stream().collect( Collectors.groupingBy( e -> e.key ) )
      .forEach( ( key, list ) -> list.stream()
          .max( Comparator.comparing( e -> e.precedence ) )
          .ifPresent( e -> consumer.accept( key, e.uri ) ) );
}

代码示例来源:origin: signalapp/Signal-Server

private void addTallyMaps(Map<String, long[]> tallyMap, Map<String, long[]> incrementMap) {
 incrementMap.forEach((key, increments) -> {
   long[] tallies = tallyMap.get(key);
   if (tallies == null) {
    tallyMap.put(key, increments);
   } else {
    for (int i = 0; i < INTERVALS.length; i++) {
     tallies[i] += increments[i];
    }
   }
  });
}

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

/**
 * Transitively retrieve all aliases for the given name.
 * @param name the target name to find aliases for
 * @param result the resulting aliases list
 */
private void retrieveAliases(String name, List<String> result) {
  this.aliasMap.forEach((alias, registeredName) -> {
    if (registeredName.equals(name)) {
      result.add(alias);
      retrieveAliases(alias, result);
    }
  });
}

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

private ModelFactory getModelFactory(HandlerMethod handlerMethod, WebDataBinderFactory binderFactory) {
  SessionAttributesHandler sessionAttrHandler = getSessionAttributesHandler(handlerMethod);
  Class<?> handlerType = handlerMethod.getBeanType();
  Set<Method> methods = this.modelAttributeCache.get(handlerType);
  if (methods == null) {
    methods = MethodIntrospector.selectMethods(handlerType, MODEL_ATTRIBUTE_METHODS);
    this.modelAttributeCache.put(handlerType, methods);
  }
  List<InvocableHandlerMethod> attrMethods = new ArrayList<>();
  // Global methods first
  this.modelAttributeAdviceCache.forEach((clazz, methodSet) -> {
    if (clazz.isApplicableToBeanType(handlerType)) {
      Object bean = clazz.resolveBean();
      for (Method method : methodSet) {
        attrMethods.add(createModelAttributeMethod(binderFactory, bean, method));
      }
    }
  });
  for (Method method : methods) {
    Object bean = handlerMethod.getBean();
    attrMethods.add(createModelAttributeMethod(binderFactory, bean, method));
  }
  return new ModelFactory(attrMethods, binderFactory, sessionAttrHandler);
}

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

/**
 * Copy constructor which allows for ignoring certain entries.
 * Used for serialization without non-serializable entries.
 * @param original the MessageHeaders to copy
 * @param keysToIgnore the keys of the entries to ignore
 */
private MessageHeaders(MessageHeaders original, Set<String> keysToIgnore) {
  this.headers = new HashMap<>(original.headers.size());
  original.headers.forEach((key, value) -> {
    if (!keysToIgnore.contains(key)) {
      this.headers.put(key, value);
    }
  });
}

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

/**
 * Construct a new MutablePropertyValues object from a Map.
 * @param original a Map with property values keyed by property name Strings
 * @see #addPropertyValues(Map)
 */
public MutablePropertyValues(@Nullable Map<?, ?> original) {
  // We can optimize this because it's all new:
  // There is no replacement of existing property values.
  if (original != null) {
    this.propertyValueList = new ArrayList<>(original.size());
    original.forEach((attrName, attrValue) -> this.propertyValueList.add(
        new PropertyValue(attrName.toString(), attrValue)));
  }
  else {
    this.propertyValueList = new ArrayList<>(0);
  }
}

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

public void setNotificationInfoMappings(Map<String, Object> notificationInfoMappings) {
  notificationInfoMappings.forEach((beanKey, result) ->
      this.notificationInfoMappings.put(beanKey, extractNotificationMetadata(result)));
}

相关文章