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

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

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

Map.put介绍

[英]Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if #containsKey(Object) would return true.)
[中]将指定值与此映射中的指定键关联(可选操作)。如果映射以前包含键的映射,则旧值将替换为指定值。(称映射m包含键k的映射,当且仅当#containsKey(Object)返回true。)

代码示例

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

private static <T, M extends Map<T, String>> M populate(M map, Entry<T, String>[] entries) {
 for (Entry<T, String> entry : entries) {
  map.put(entry.getKey(), entry.getValue());
 }
 return map;
}

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

/** An implementation of {@link Map#putAll}. */
static <K, V> void putAllImpl(Map<K, V> self, Map<? extends K, ? extends V> map) {
 for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
  self.put(entry.getKey(), entry.getValue());
 }
}

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

private Map<C, V> getOrCreate(R rowKey) {
 Map<C, V> map = backingMap.get(rowKey);
 if (map == null) {
  map = factory.get();
  backingMap.put(rowKey, map);
 }
 return map;
}

代码示例来源:origin: square/retrofit

void addContributor(String owner, String repo, String name, int contributions) {
  Map<String, List<Contributor>> repoContributors = ownerRepoContributors.get(owner);
  if (repoContributors == null) {
   repoContributors = new LinkedHashMap<>();
   ownerRepoContributors.put(owner, repoContributors);
  }
  List<Contributor> contributors = repoContributors.get(repo);
  if (contributors == null) {
   contributors = new ArrayList<>();
   repoContributors.put(repo, contributors);
  }
  contributors.add(new Contributor(name, contributions));
 }
}

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

private byte[] encodeHeaderKey(String input, boolean escape) {
  String inputToUse = (escape ? escape(input) : input);
  if (this.headerKeyAccessCache.containsKey(inputToUse)) {
    return this.headerKeyAccessCache.get(inputToUse);
  }
  synchronized (this.headerKeyUpdateCache) {
    byte[] bytes = this.headerKeyUpdateCache.get(inputToUse);
    if (bytes == null) {
      bytes = inputToUse.getBytes(StandardCharsets.UTF_8);
      this.headerKeyAccessCache.put(inputToUse, bytes);
      this.headerKeyUpdateCache.put(inputToUse, bytes);
    }
    return bytes;
  }
}

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

private void modifyEnvironment(Map<String, String> buildEnv) {
  for (Map.Entry<String, String> entry : env.entrySet()) {
    if ("PATH".equals(entry.getKey())) {
      buildEnv.put("PATH", buildEnv.get("PATH") + File.pathSeparatorChar + env.get("PATH"));
    } else {
      buildEnv.put(entry.getKey(), entry.getValue());
    }
  }
}

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

@Override
public void set(K key, @Nullable V value) {
  List<V> values = new LinkedList<>();
  values.add(value);
  this.map.put(key, values);
}

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

private void innerUpdateEndOffsets(final Map<TopicPartition, Long> newOffsets,
                  final boolean replace) {
  for (final Map.Entry<TopicPartition, Long> entry : newOffsets.entrySet()) {
    List<Long> offsets = endOffsets.get(entry.getKey());
    if (replace || offsets == null) {
      offsets = new ArrayList<>();
    }
    offsets.add(entry.getValue());
    endOffsets.put(entry.getKey(), offsets);
  }
}

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

/**
 * Add an option argument for the given option name and add the given value to the
 * list of values associated with this option (of which there may be zero or more).
 * The given value may be {@code null}, indicating that the option was specified
 * without an associated value (e.g. "--foo" vs. "--foo=bar").
 */
public void addOptionArg(String optionName, @Nullable String optionValue) {
  if (!this.optionArgs.containsKey(optionName)) {
    this.optionArgs.put(optionName, new ArrayList<>());
  }
  if (optionValue != null) {
    this.optionArgs.get(optionName).add(optionValue);
  }
}

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

private void createConnectionsMaxReauthMsMap(Map<String, ?> configs) {
  for (String mechanism : jaasContexts.keySet()) {
    String prefix = ListenerName.saslMechanismPrefix(mechanism);
    Long connectionsMaxReauthMs = (Long) configs.get(prefix + BrokerSecurityConfigs.CONNECTIONS_MAX_REAUTH_MS);
    if (connectionsMaxReauthMs == null)
      connectionsMaxReauthMs = (Long) configs.get(BrokerSecurityConfigs.CONNECTIONS_MAX_REAUTH_MS);
    if (connectionsMaxReauthMs != null)
      connectionsMaxReauthMsByMechanism.put(mechanism, connectionsMaxReauthMs);
  }
}

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

@Override
 public Iterable<Entry<E>> order(List<Entry<E>> insertionOrder) {
  // We mimic the order from gen.
  Map<E, Entry<E>> map = new LinkedHashMap<>();
  for (Entry<E> entry : insertionOrder) {
   map.put(entry.getElement(), entry);
  }
  Set<E> seen = new HashSet<>();
  List<Entry<E>> order = new ArrayList<>();
  for (E e : gen.order(new ArrayList<E>(map.keySet()))) {
   if (seen.add(e)) {
    order.add(map.get(e));
   }
  }
  return order;
 }
}

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

@Override
public boolean setInitParameter(String name, String value) {
  Assert.notNull(name, "Parameter name must not be null");
  if (this.initParameters.containsKey(name)) {
    return false;
  }
  this.initParameters.put(name, value);
  return true;
}

代码示例来源:origin: alibaba/druid

private void checkAllDataSources() {
  Map<String, DataSource> dataSourceMap = selector.getDataSourceMap();
  for (Map.Entry<String, DataSource> e : dataSourceMap.entrySet()) {
    if (!(e.getValue() instanceof DruidDataSource)) {
      continue;
    }
    boolean flag = check(e.getKey(), (DruidDataSource) e.getValue());
    if (flag) {
      errorCounts.put(e.getKey(), 0);
    } else {
      if (!errorCounts.containsKey(e.getKey())) {
        errorCounts.put(e.getKey(), 0);
      }
      int count = errorCounts.get(e.getKey());
      errorCounts.put(e.getKey(), count + 1);
    }
  }
}

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

private void updateBindingContext(BindingContext context, ServerWebExchange exchange) {
  Map<String, Object> model = context.getModel().asMap();
  model.keySet().stream()
      .filter(name -> isBindingCandidate(name, model.get(name)))
      .filter(name -> !model.containsKey(BindingResult.MODEL_KEY_PREFIX + name))
      .forEach(name -> {
        WebExchangeDataBinder binder = context.createDataBinder(exchange, model.get(name), name);
        model.put(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
      });
}

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

private void putAll(Iterable<Entry<K, V>> entries) {
 Map<K, V> map = new LinkedHashMap<>();
 for (Entry<K, V> entry : entries) {
  map.put(entry.getKey(), entry.getValue());
 }
 getMap().putAll(map);
}

代码示例来源:origin: alibaba/druid

@SuppressWarnings("unchecked")
public void addAfterComment(String comment) {
  if (attributes == null) {
    attributes = new HashMap<String, Object>(1);
  }
  
  List<String> comments = (List<String>) attributes.get("format.after_comment");
  if (comments == null) {
    comments = new ArrayList<String>(2);
    attributes.put("format.after_comment", comments);
  }
  
  comments.add(comment);
}

代码示例来源:origin: Tencent/tinker

void putSanitizeName(RType rType, String sanitizeName, String rawName) {
  HashMap<String, String> sanitizeNameMap;
  if (!sanitizeTypeMap.containsKey(rType)) {
    sanitizeNameMap = new HashMap<>();
    sanitizeTypeMap.put(rType, sanitizeNameMap);
  } else {
    sanitizeNameMap = sanitizeTypeMap.get(rType);
  }
  if (!sanitizeNameMap.containsKey(sanitizeName)) {
    sanitizeNameMap.put(sanitizeName, rawName);
  }
}

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

@Override
public BenchmarkResultHook addResults(Map<String, Long> results)
{
  requireNonNull(results, "results is null");
  for (Entry<String, Long> entry : results.entrySet()) {
    Long currentSum = resultsSum.get(entry.getKey());
    if (currentSum == null) {
      currentSum = 0L;
    }
    resultsSum.put(entry.getKey(), currentSum + entry.getValue());
  }
  resultsCount++;
  return this;
}

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

/**
 * put all.
 *
 * @param map map.
 */
public void putAll(Map<String, Object> map) {
  for (Map.Entry<String, Object> entry : map.entrySet()) {
    mMap.put(entry.getKey(), entry.getValue());
  }
}

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

@Override
public void set(K key, @Nullable V value) {
  List<V> values = new LinkedList<>();
  values.add(value);
  this.targetMap.put(key, values);
}

相关文章