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

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

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

Map.containsKey介绍

[英]Returns whether this Map contains the specified key.
[中]

代码示例

代码示例来源: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: spring-projects/spring-framework

@Override
  @Nullable
  public Object getValue(@Nullable String name) {
    if (!this.uriVariables.containsKey(name)) {
      throw new IllegalArgumentException("Map has no value for '" + name + "'");
    }
    return this.uriVariables.get(name);
  }
}

代码示例来源: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: 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: spring-projects/spring-framework

/**
 * Determine if the parameters in this {@code MimeType} and the supplied
 * {@code MimeType} are equal, performing case-insensitive comparisons
 * for {@link Charset Charsets}.
 * @since 4.2
 */
private boolean parametersAreEqual(MimeType other) {
  if (this.parameters.size() != other.parameters.size()) {
    return false;
  }
  for (Map.Entry<String, String> entry : this.parameters.entrySet()) {
    String key = entry.getKey();
    if (!other.parameters.containsKey(key)) {
      return false;
    }
    if (PARAM_CHARSET.equals(key)) {
      if (!ObjectUtils.nullSafeEquals(getCharset(), other.getCharset())) {
        return false;
      }
    }
    else if (!ObjectUtils.nullSafeEquals(entry.getValue(), other.parameters.get(key))) {
      return false;
    }
  }
  return true;
}

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

private List<Node> convertToNodes(Map<Integer, Node> brokers, Object[] brokerIds) {
  List<Node> nodes = new ArrayList<>(brokerIds.length);
  for (Object brokerId : brokerIds)
    if (brokers.containsKey(brokerId))
      nodes.add(brokers.get(brokerId));
    else
      nodes.add(new Node((int) brokerId, "", -1));
  return nodes;
}

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

private static Map<String, Object> combineProperties(Set<String> specifiedPropertyKeys, Map<String, Object> defaultProperties, Map<String, Object> inheritedProperties)
  {
    Map<String, Object> finalProperties = new HashMap<>(inheritedProperties);
    for (Map.Entry<String, Object> entry : defaultProperties.entrySet()) {
      if (specifiedPropertyKeys.contains(entry.getKey()) || !finalProperties.containsKey(entry.getKey())) {
        finalProperties.put(entry.getKey(), entry.getValue());
      }
    }
    return finalProperties;
  }
}

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

public ConfigDef define(ConfigKey key) {
  if (configKeys.containsKey(key.name)) {
    throw new ConfigException("Configuration " + key.name + " is defined twice.");
  }
  if (key.group != null && !groups.contains(key.group)) {
    groups.add(key.group);
  }
  configKeys.put(key.name, key);
  return this;
}

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

private static int mergeMaps(Map<String, Integer> map, Map<String, Integer> other)
{
  int deltaSize = 0;
  for (Map.Entry<String, Integer> entry : other.entrySet()) {
    if (!map.containsKey(entry.getKey())) {
      deltaSize += entry.getKey().getBytes().length + SIZE_OF_INT;
    }
    map.put(entry.getKey(), map.getOrDefault(entry.getKey(), 0) + other.getOrDefault(entry.getKey(), 0));
  }
  return deltaSize;
}

代码示例来源: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: apache/incubator-druid

GcCounters()
{
 // connect to itself
 final JStatData jStatData = JStatData.connect(pid);
 final Map<String, JStatData.Counter<?>> jStatCounters = jStatData.getAllCounters();
 generations.add(new GcGeneration(jStatCounters, 0, "young"));
 generations.add(new GcGeneration(jStatCounters, 1, "old"));
 // Removed in Java 8 but still actual for previous Java versions
 if (jStatCounters.containsKey("sun.gc.generation.2.name")) {
  generations.add(new GcGeneration(jStatCounters, 2, "perm"));
 }
}

代码示例来源:origin: Netflix/eureka

public StringInterningAmazonInfoBuilder withMetadata(HashMap<String, String> metadata) {
  this.metadata = metadata;
  if (metadata.isEmpty()) {
    return this;
  }
  for (Map.Entry<String, String> entry : metadata.entrySet()) {
    String key = entry.getKey().intern();
    if (VALUE_INTERN_KEYS.containsKey(key)) {
      entry.setValue(StringCache.intern(entry.getValue()));
    }
  }
  return this;
}

代码示例来源:origin: stanfordnlp/CoreNLP

private static void addToMatchedTokensByPhrase(String ph, String sentid, int index, int length){
 if(!Data.matchedTokensForEachPhrase.containsKey(ph))
  Data.matchedTokensForEachPhrase.put(ph, new HashMap<>());
 Map<String, List<Integer>> matcheds = Data.matchedTokensForEachPhrase.get(ph);
 if(!matcheds.containsKey(sentid))
  matcheds.put(sentid, new ArrayList<>());
 for (int i = 0; i < length; i++)
  matcheds.get(sentid).add(index + i);
}

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

private Object getIntroductionDelegateFor(Object targetObject) {
  synchronized (this.delegateMap) {
    if (this.delegateMap.containsKey(targetObject)) {
      return this.delegateMap.get(targetObject);
    }
    else {
      Object delegate = createNewDelegate();
      this.delegateMap.put(targetObject, delegate);
      return delegate;
    }
  }
}

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

@Override
public V apply(@Nullable K key) {
 V result = map.get(key);
 return (result != null || map.containsKey(key)) ? result : defaultValue;
}

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

private static boolean areSelectionOverridesEqual(
   Map<TrackGroupArray, SelectionOverride> first,
   Map<TrackGroupArray, SelectionOverride> second) {
  int firstSize = first.size();
  if (second.size() != firstSize) {
   return false;
  }
  for (Map.Entry<TrackGroupArray, SelectionOverride> firstEntry : first.entrySet()) {
   TrackGroupArray key = firstEntry.getKey();
   if (!second.containsKey(key) || !Util.areEqual(firstEntry.getValue(), second.get(key))) {
    return false;
   }
  }
  return true;
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
public void process(int id, Document document) {
 if (dataset.containsKey(id)) {
  documents.add(extractor.extract(id, document, dataset.get(id)));
 }
}

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

public void setAttachmentIfAbsent(String key, String value) {
  if (attachments == null) {
    attachments = new HashMap<String, String>();
  }
  if (!attachments.containsKey(key)) {
    attachments.put(key, value);
  }
}

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

private static <K, V> void doDifference(
  Map<? extends K, ? extends V> left,
  Map<? extends K, ? extends V> right,
  Equivalence<? super V> valueEquivalence,
  Map<K, V> onlyOnLeft,
  Map<K, V> onlyOnRight,
  Map<K, V> onBoth,
  Map<K, MapDifference.ValueDifference<V>> differences) {
 for (Entry<? extends K, ? extends V> entry : left.entrySet()) {
  K leftKey = entry.getKey();
  V leftValue = entry.getValue();
  if (right.containsKey(leftKey)) {
   V rightValue = onlyOnRight.remove(leftKey);
   if (valueEquivalence.equivalent(leftValue, rightValue)) {
    onBoth.put(leftKey, leftValue);
   } else {
    differences.put(leftKey, ValueDifferenceImpl.create(leftValue, rightValue));
   }
  } else {
   onlyOnLeft.put(leftKey, leftValue);
  }
 }
}

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

/**
 * Determine whether this registry contains a custom editor
 * for the specified array/collection element.
 * @param elementType the target type of the element
 * (can be {@code null} if not known)
 * @param propertyPath the property path (typically of the array/collection;
 * can be {@code null} if not known)
 * @return whether a matching custom editor has been found
 */
public boolean hasCustomEditorForElement(@Nullable Class<?> elementType, @Nullable String propertyPath) {
  if (propertyPath != null && this.customEditorsForPath != null) {
    for (Map.Entry<String, CustomEditorHolder> entry : this.customEditorsForPath.entrySet()) {
      if (PropertyAccessorUtils.matchesProperty(entry.getKey(), propertyPath) &&
          entry.getValue().getPropertyEditor(elementType) != null) {
        return true;
      }
    }
  }
  // No property-specific editor -> check type-specific editor.
  return (elementType != null && this.customEditors != null && this.customEditors.containsKey(elementType));
}

相关文章