java.util.HashMap.<init>()方法的使用及代码示例

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

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

HashMap.<init>介绍

[英]Constructs a new empty HashMap instance.
[中]构造一个新的空HashMap实例。

代码示例

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

private static <K, V> HashMap<K, V> newHashMap(
   Collection<? extends Entry<? extends K, ? extends V>> entries) {
  HashMap<K, V> map = new HashMap<>();
  for (Entry<? extends K, ? extends V> entry : entries) {
   map.put(entry.getKey(), entry.getValue());
  }
  return map;
 }
}

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

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

// Iterating over keys only
for (Integer key : map.keySet()) {
  System.out.println("Key = " + key);
}

// Iterating over values only
for (Integer value : map.values()) {
  System.out.println("Value = " + value);
}

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

public int execute(int intIn) {
    Map<String, Integer> in = new HashMap<>();
    in.put("intIn", intIn);
    Map<String, Object> out = execute(in);
    return ((Number) out.get("intOut")).intValue();
  }
}

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

Map<String, String> map = new HashMap<String, String>();
map.put("dog", "type of animal");
System.out.println(map.get("dog"));

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

public static void main(String[] args) {
  Map<String,Double> variables = new HashMap<>();
  Expression exp = parse("x^2 - x + 2", variables);
  for (double x = -20; x <= +20; x++) {
    variables.put("x", x);
    System.out.println(x + " => " + exp.eval());
  }
}

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

private List<Invoker<T>> toMergeInvokerList(List<Invoker<T>> invokers) {
  List<Invoker<T>> mergedInvokers = new ArrayList<>();
  Map<String, List<Invoker<T>>> groupMap = new HashMap<String, List<Invoker<T>>>();
  for (Invoker<T> invoker : invokers) {
    String group = invoker.getUrl().getParameter(Constants.GROUP_KEY, "");
    groupMap.computeIfAbsent(group, k -> new ArrayList<>());
    groupMap.get(group).add(invoker);
  }
  if (groupMap.size() == 1) {
    mergedInvokers.addAll(groupMap.values().iterator().next());
  } else if (groupMap.size() > 1) {
    for (List<Invoker<T>> groupList : groupMap.values()) {
      StaticDirectory<T> staticDirectory = new StaticDirectory<>(groupList);
      staticDirectory.buildRouterChain();
      mergedInvokers.add(cluster.join(staticDirectory));
    }
  } else {
    mergedInvokers = invokers;
  }
  return mergedInvokers;
}

代码示例来源:origin: ReactiveX/RxJava

private static Map<String, String> getMap(String prefix) {
  Map<String, String> m = new HashMap<String, String>();
  m.put("firstName", prefix + "First");
  m.put("lastName", prefix + "Last");
  return m;
}

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

public Map<CaseInsensitiveString, List<CaseInsensitiveString>> templatesWithPipelinesForUser(CaseInsensitiveString username) {
  HashMap<CaseInsensitiveString, List<CaseInsensitiveString>> templatesToPipelinesMap = new HashMap<>();
  Map<CaseInsensitiveString, Map<CaseInsensitiveString, Authorization>> authMap = goConfigService.getCurrentConfig().templatesWithAssociatedPipelines();
  for (CaseInsensitiveString templateName : authMap.keySet()) {
    if (securityService.isAuthorizedToViewTemplate(templateName, new Username(username))) {
      templatesToPipelinesMap.put(templateName, new ArrayList<>());
      Map<CaseInsensitiveString, Authorization> authorizationMap = authMap.get(templateName);
      for (CaseInsensitiveString pipelineName : authorizationMap.keySet()) {
        templatesToPipelinesMap.get(templateName).add(pipelineName);
      }
    }
  }
  return templatesToPipelinesMap;
}

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

public static <V> HashMap<V, Integer> multi_set(List<V> list) {
  HashMap<V, Integer> rtn = new HashMap<>();
  for (V v : list) {
    int cnt = 1;
    if (rtn.containsKey(v)) {
      cnt += rtn.get(v);
    }
    rtn.put(v, cnt);
  }
  return rtn;
}

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

@Test
public void testIsEmpty() {
  assertTrue(CollectionUtils.isEmpty((Set<Object>) null));
  assertTrue(CollectionUtils.isEmpty((Map<String, String>) null));
  assertTrue(CollectionUtils.isEmpty(new HashMap<String, String>()));
  assertTrue(CollectionUtils.isEmpty(new HashSet<>()));
  List<Object> list = new LinkedList<>();
  list.add(new Object());
  assertFalse(CollectionUtils.isEmpty(list));
  Map<String, String> map = new HashMap<>();
  map.put("foo", "bar");
  assertFalse(CollectionUtils.isEmpty(map));
}

代码示例来源:origin: bumptech/glide

private Map<String, List<LazyHeaderFactory>> copyHeaders() {
 Map<String, List<LazyHeaderFactory>> result = new HashMap<>(headers.size());
 for (Map.Entry<String, List<LazyHeaderFactory>> entry : headers.entrySet()) {
  @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
  List<LazyHeaderFactory> valueCopy = new ArrayList<>(entry.getValue());
  result.put(entry.getKey(), valueCopy);
 }
 return result;
}

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

HashMap<String, Double> map = new HashMap<String, Double>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);
map.put("A", 99.5);
map.put("B", 67.4);
map.put("C", 67.4);
map.put("D", 67.3);
System.out.println("unsorted map: " + map);
sorted_map.putAll(map);
System.out.println("results: " + sorted_map);
if (base.get(a) >= base.get(b)) {
  return -1;
} else {

代码示例来源:origin: airbnb/lottie-android

@SuppressWarnings("SameParameterValue")
private LottieComposition createComposition(int startFrame, int endFrame) {
 LottieComposition composition = new LottieComposition();
 composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<Layer>(),
     new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0),
     new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0),
     new HashMap<String, Font>(0));
 return composition;
}

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

@Test
public void doNotCopyAttributes() throws Exception {
  Map<String, Object> attributes = new HashMap<>();
  WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
  this.servletRequest.setSession(new MockHttpSession(null, "123"));
  this.servletRequest.getSession().setAttribute("foo", "bar");
  HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
  interceptor.setCopyAllAttributes(false);
  interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);
  assertEquals(1, attributes.size());
  assertEquals("123", attributes.get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME));
}

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

@Override
public Map<Errors, Integer> errorCounts() {
  HashMap<Errors, Integer> counts = new HashMap<>();
  for (ReplicaElectionResult result : data.replicaElectionResults()) {
    for (PartitionResult partitionResult : result.partitionResult()) {
      Errors error = Errors.forCode(partitionResult.errorCode());
      counts.put(error, counts.getOrDefault(error, 0) + 1);
    }
  }
  return counts;
}

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

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

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

public int execute(int amount, int custid) {
    Map<String, Integer> in = new HashMap<>();
    in.put("amount", amount);
    in.put("custid", custid);
    Map<String, Object> out = execute(in);
    return ((Number) out.get("newid")).intValue();
  }
}

相关文章