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

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

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

Map.get介绍

[英]Returns the value of the mapping with the specified key.
[中]返回具有指定键的映射的值。

代码示例

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: square/okhttp

@Override public List<InetAddress> lookup(String hostname) throws UnknownHostException {
 requestedHosts.add(hostname);
 List<InetAddress> result = hostAddresses.get(hostname);
 if (result != null) return result;
 throw new UnknownHostException();
}

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

ServiceMethod<?> loadServiceMethod(Method method) {
 ServiceMethod<?> result = serviceMethodCache.get(method);
 if (result != null) return result;
 synchronized (serviceMethodCache) {
  result = serviceMethodCache.get(method);
  if (result == null) {
   result = ServiceMethod.parseAnnotations(this, method);
   serviceMethodCache.put(method, result);
  }
 }
 return result;
}

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

static String extractStatusLine(Map<String, List<String>> javaResponseHeaders)
  throws ProtocolException {
 List<String> values = javaResponseHeaders.get(null);
 if (values == null || values.size() == 0) {
  // The status line is missing. This suggests a badly behaving cache.
  throw new ProtocolException(
    "CacheResponse is missing a \'null\' header containing the status line. Headers="
      + javaResponseHeaders);
 }
 return values.get(0);
}

代码示例来源: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/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: airbnb/lottie-android

private List<ContentGroup> getContentsForCharacter(FontCharacter character) {
 if (contentsForCharacter.containsKey(character)) {
  return contentsForCharacter.get(character);
 }
 List<ShapeGroup> shapes = character.getShapes();
 int size = shapes.size();
 List<ContentGroup> contents = new ArrayList<>(size);
 for (int i = 0; i < size; i++) {
  ShapeGroup sg = shapes.get(i);
  contents.add(new ContentGroup(lottieDrawable, this, sg));
 }
 contentsForCharacter.put(character, contents);
 return contents;
}

代码示例来源:origin: jenkinsci/jenkins

private List<AbstractProject> get(Map<AbstractProject, List<DependencyGroup>> map, AbstractProject src, boolean up) {
  List<DependencyGroup> v = map.get(src);
  if(v==null) return Collections.emptyList();
  List<AbstractProject> result = new ArrayList<AbstractProject>(v.size());
  for (DependencyGroup d : v) result.add(up ? d.getUpstreamProject() : d.getDownstreamProject());
  return result;
}

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

public static String getVerticalFormattedOutput(List<Map<String, Object>> content, String[] titleFields) {
  List<String[]> printContents = new ArrayList<String[]>();
  int maxCol = content.size() > MAX_COL ? MAX_COL : content.size();
  for (String titleField : titleFields) {
    String[] row = new String[maxCol + 1];
    row[0] = titleField;
    for (int j = 0; j < maxCol; j++) {
      Map<String, Object> sqlStat = content.get(j);
      Object value = sqlStat.get(titleField);
      row[j + 1] = handleAndConvert(value, titleField);
    }
    printContents.add(row);
  }
  return TableFormatter.format(printContents);
}

代码示例来源:origin: hankcs/HanLP

public int addCategory(String category)
{
  Integer id = categoryId.get(category);
  if (id == null)
  {
    id = categoryId.size();
    categoryId.put(category, id);
    assert idCategory.size() == id;
    idCategory.add(category);
  }
  return id;
}

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

@Test
public void testGenericTypeNestingListOfMapOfInteger() throws Exception {
  List<Map<String, String>> list = new LinkedList<>();
  Map<String, String> map = new HashMap<>();
  map.put("testKey", "5");
  list.add(map);
  NestedGenericCollectionBean gb = new NestedGenericCollectionBean();
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfMapOfInteger", list);
  Object obj = gb.getListOfMapOfInteger().get(0).get("testKey");
  assertTrue(obj instanceof Integer);
  assertEquals(5, ((Integer) obj).intValue());
}

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

@Implementation(minSdk = JELLY_BEAN_MR2)
protected Account[] getAccountsByTypeForPackage(String type, String packageName) {
 List<Account> result = new ArrayList<>();
 Account[] accountsByType = getAccountsByType(type);
 for (Account account : accountsByType) {
  if (packageVisibileAccounts.containsKey(account) && packageVisibileAccounts.get(account).contains(packageName)) {
   result.add(account);
  }
 }
 return result.toArray(new Account[result.size()]);
}

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

@Nullable
private Object getNativeHeaderValue(Message<?> message, String name) {
  Map<String, List<String>> nativeHeaders = getNativeHeaders(message);
  if (name.startsWith("nativeHeaders.")) {
    name = name.substring("nativeHeaders.".length());
  }
  if (nativeHeaders == null || !nativeHeaders.containsKey(name)) {
    return null;
  }
  List<?> nativeHeaderValues = nativeHeaders.get(name);
  return (nativeHeaderValues.size() == 1 ? nativeHeaderValues.get(0) : nativeHeaderValues);
}

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

@Test
public void testGenericListOfMaps() throws MalformedURLException {
  GenericBean<String> gb = new GenericBean<>();
  List<Map<Integer, Long>> list = new LinkedList<>();
  list.add(new HashMap<>());
  gb.setListOfMaps(list);
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("listOfMaps[0][10]", new Long(5));
  assertEquals(new Long(5), bw.getPropertyValue("listOfMaps[0][10]"));
  assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
}

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

@Override
  protected List<Entry> buildFeedEntries(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) {
    List<Entry> entries = new ArrayList<>();
    for (String name : model.keySet()) {
      Entry entry = new Entry();
      entry.setTitle(name);
      Content content = new Content();
      content.setValue((String) model.get(name));
      entry.setSummary(content);
      entries.add(entry);
    }
    return entries;
  }
}

相关文章