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

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

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

Map.keySet介绍

[英]Returns a set of the keys contained in this Map. The Set is backed by this Map so changes to one are reflected by the other. The Set does not support adding.
[中]返回此映射中包含的一组键。该集合由该映射支持,因此对其中一个的更改将由另一个映射反映。集合不支持添加。

代码示例

canonical example by Tabnine

private void mappingWordsLength(List<String> wordsList) {
 Map<Integer, Set<String>> mapping = new HashMap<>();
 for (String word : wordsList) {
  mapping.computeIfAbsent(word.length(), HashSet::new).add(word);
 }
 List<Integer> lengths = new LinkedList<>(mapping.keySet());
 Collections.sort(lengths);
 lengths.forEach(n -> System.out.println(mapping.get(n).size() + " words with " + n + " chars"));
}

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

@Override
 public Iterator<C> apply(Map<C, V> input) {
  return input.keySet().iterator();
 }
}),

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

/**
 * If the name is the expected name specified in the constructor, return the
 * object provided in the constructor. If the name is unexpected, a
 * respective NamingException gets thrown.
 */
@Override
public Object lookup(String name) throws NamingException {
  Object object = this.jndiObjects.get(name);
  if (object == null) {
    throw new NamingException("Unexpected JNDI name '" + name + "': expecting " + this.jndiObjects.keySet());
  }
  return object;
}

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

@Override public void deleteContents(File directory) throws IOException {
  String prefix = directory.toString() + "/";
  for (Iterator<File> i = files.keySet().iterator(); i.hasNext(); ) {
   File file = i.next();
   if (file.toString().startsWith(prefix)) i.remove();
  }
 }
}

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

public static Map<String, Long> subtract(Map<String, Long> xs, Map<String, Long> ys)
 {
  assert xs.keySet().equals(ys.keySet());
  final Map<String, Long> zs = new HashMap<String, Long>();
  for (String k : xs.keySet()) {
   zs.put(k, xs.get(k) - ys.get(k));
  }
  return zs;
 }
}

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

public List<String> getFilenames() {
  List<String> filenames = new ArrayList<>();
  for(String keyForFile : outFilenames.keySet())
   filenames.add(outFilenames.get(keyForFile));
  return filenames;
 }
}

代码示例来源: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: square/okhttp

@Override public void testRunFinished(Result result) throws Exception {
  Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler);
  System.err.println("Uninstalled aggressive uncaught exception handler");

  synchronized (exceptions) {
   if (!exceptions.isEmpty()) {
    throw Throwables.rethrowAsException(exceptions.keySet().iterator().next());
   }
  }
 }
}

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

@Override
public List<URL> lookup(URL url) {
  List<URL> urls = new ArrayList<>();
  Map<String, List<URL>> notifiedUrls = getNotified().get(url);
  if (notifiedUrls != null && notifiedUrls.size() > 0) {
    for (List<URL> values : notifiedUrls.values()) {
    for (URL u : getRegistered()) {
      if (UrlUtils.isMatch(url, u)) {
        urls.add(u);
    for (URL u : getSubscribed().keySet()) {
      if (UrlUtils.isMatch(url, u)) {
        urls.add(u);

代码示例来源:origin: ctripcorp/apollo

private Set<String> stringPropertyNames(Properties properties) {
 //jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历.
 Map<String, String> h = new HashMap<>();
 for (Map.Entry<Object, Object> e : properties.entrySet()) {
  Object k = e.getKey();
  Object v = e.getValue();
  if (k instanceof String && v instanceof String) {
   h.put((String) k, (String) v);
  }
 }
 return h.keySet();
}

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

public static void reverse(Map source, Map target) {
  for (Iterator it = source.keySet().iterator(); it.hasNext();) {
    Object key = it.next();
    target.put(source.get(key), key);
  }
}

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

/**
 * Return the {@link Method} mapped to the given exception type, or {@code null} if none.
 */
@Nullable
private Method getMappedMethod(Class<? extends Throwable> exceptionType) {
  List<Class<? extends Throwable>> matches = new ArrayList<>();
  for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) {
    if (mappedException.isAssignableFrom(exceptionType)) {
      matches.add(mappedException);
    }
  }
  if (!matches.isEmpty()) {
    matches.sort(new ExceptionDepthComparator(exceptionType));
    return this.mappedMethods.get(matches.get(0));
  }
  else {
    return null;
  }
}

代码示例来源:origin: Alluxio/alluxio

@Override
public Map<String, List<String>> getDirectoryPathsOnTiers() {
 Map<String, List<String>> pathsOnTiers = new HashMap<>();
 for (Pair<String, String> tierPath : mCapacityBytesOnDirs.keySet()) {
  String tier = tierPath.getFirst();
  if (pathsOnTiers.get(tier) == null) {
   pathsOnTiers.put(tier, new ArrayList<String>());
  }
  pathsOnTiers.get(tier).add(tierPath.getSecond());
 }
 return pathsOnTiers;
}

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

static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) {
 if (map instanceof EnumBiMap) {
  return ((EnumBiMap<K, ?>) map).keyType();
 }
 if (map instanceof EnumHashBiMap) {
  return ((EnumHashBiMap<K, ?>) map).keyType();
 }
 checkArgument(!map.isEmpty());
 return map.keySet().iterator().next().getDeclaringClass();
}

代码示例来源:origin: stackoverflow.com

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
  Integer value = map.get(key);
  System.out.println("Key = " + key + ", Value = " + value);
}

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

@Override
public List<URL> lookup(URL url) {
  List<URL> urls = new ArrayList<>();
  Map<String, List<URL>> notifiedUrls = getNotified().get(url);
  if (notifiedUrls != null && notifiedUrls.size() > 0) {
    for (List<URL> values : notifiedUrls.values()) {
    for (URL u : getRegistered()) {
      if (UrlUtils.isMatch(url, u)) {
        urls.add(u);
    for (URL u : getSubscribed().keySet()) {
      if (UrlUtils.isMatch(url, u)) {
        urls.add(u);

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

@Override
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
    throws BeansException {
  Map<String, Object> results = new LinkedHashMap<>();
  for (String beanName : this.beans.keySet()) {
    if (findAnnotationOnBean(beanName, annotationType) != null) {
      results.put(beanName, getBean(beanName));
    }
  }
  return results;
}

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

/**
 * Return all declared prefixes.
 */
public Iterator<String> getBoundPrefixes() {
  return this.prefixToNamespaceUri.keySet().iterator();
}

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

private void filterFeatures(Set<String> keep) {
 Iterator<String> featureIt = featureWeights.keySet().iterator();
 while (featureIt.hasNext()) {
  if (!keep.contains(featureIt.next())) {
   featureIt.remove();
  }
 }
}

相关文章