java.util.HashMap类的使用及代码示例

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

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

HashMap介绍

[英]HashMap is an implementation of Map. All optional operations are supported.

All elements are permitted as keys or values, including null.

Note that the iteration order for HashMap is non-deterministic. If you want deterministic iteration, use LinkedHashMap.

Note: the implementation of HashMap is not synchronized. If one thread of several threads accessing an instance modifies the map structurally, access to the map needs to be synchronized. A structural modification is an operation that adds or removes an entry. Changes in the value of an entry are not structural changes.

The Iterator created by calling the iterator method may throw a ConcurrentModificationException if the map is structurally changed while an iterator is used to iterate over the elements. Only the remove method that is provided by the iterator allows for removal of elements during iteration. It is not possible to guarantee that this mechanism works in all cases of unsynchronized concurrent modification. It should only be used for debugging purposes.
[中]HashMap是Map的一个实现。支持所有可选操作。
所有元素都允许作为键或值,包括null。
请注意,HashMap的迭代顺序是不确定的。如果您想要确定性迭代,请使用LinkedHashMap。
注意:HashMap的实现是不同步的。如果访问实例的多个线程中的一个线程在结构上修改了映射,则需要同步对映射的访问。结构修改是添加或删除条目的操作。条目值的更改不是结构更改。
通过调用迭代器方法创建的迭代器可能会抛出ConcurrentModificationException,如果在使用迭代器对元素进行迭代时映射在结构上发生了更改。只有迭代器提供的remove方法允许在迭代期间删除元素。无法保证此机制在所有非同步并发修改的情况下都有效。它只能用于调试目的。

代码示例

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: ReactiveX/RxJava

@Override public Map<Object, Object> call() throws Exception {
    return new HashMap<Object, Object>();
  }
}

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

protected void count(String methodName) {
  Integer i = map.get(methodName);
  i = (i != null) ? new Integer(i.intValue() + 1) : new Integer(1);
  map.put(methodName, i);
  ++allCount;
}

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

HashMap<String, HashMap> selects = new HashMap<String, HashMap>();

for(Map.Entry<String, HashMap> entry : selects.entrySet()) {
  String key = entry.getKey();
  HashMap value = entry.getValue();

  // do what you have to do here
  // In your case, an other loop.
}

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

@Test
public void testGenericMapWithKeyType() {
  GenericBean<?> gb = new GenericBean<>();
  BeanWrapper bw = new BeanWrapperImpl(gb);
  Map<String, String> input = new HashMap<>();
  input.put("4", "5");
  input.put("6", "7");
  bw.setPropertyValue("longMap", input);
  assertEquals("5", gb.getLongMap().get(new Long("4")));
  assertEquals("7", gb.getLongMap().get(new Long("6")));
}

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

@Test
public void shouldGetEntrySet() {
  this.map.put(123, "123");
  this.map.put(456, null);
  this.map.put(null, "789");
  HashMap<Integer, String> expected = new HashMap<>();
  expected.put(123, "123");
  expected.put(456, null);
  expected.put(null, "789");
  assertThat(this.map.entrySet(), is(expected.entrySet()));
}

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

@Override
@Nullable
public V put(String key, @Nullable V value) {
  String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
  V oldKeyValue = null;
  if (oldKey != null && !oldKey.equals(key)) {
    oldKeyValue = this.targetMap.remove(oldKey);
  }
  V oldValue = this.targetMap.put(key, value);
  return (oldKeyValue != null ? oldKeyValue : oldValue);
}

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

@Override
@Nullable
public V getOrDefault(Object key, V defaultValue) {
  if (key instanceof String) {
    String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
    if (caseInsensitiveKey != null) {
      return this.targetMap.get(caseInsensitiveKey);
    }
  }
  return defaultValue;
}

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

@Test
public void testGenericMapElementWithKeyType() {
  GenericBean<?> gb = new GenericBean<>();
  gb.setLongMap(new HashMap<Long, Integer>());
  BeanWrapper bw = new BeanWrapperImpl(gb);
  bw.setPropertyValue("longMap[4]", "5");
  assertEquals("5", gb.getLongMap().get(new Long("4")));
  assertEquals("5", bw.getPropertyValue("longMap[4]"));
}

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

@Override
public V put(K key, V value) {
  if (this.frozen) {
    throw new UnsupportedOperationException();
  }
  else {
    return super.put(key, value);
  }
}

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

@Override
@Nullable
public V get(Object key) {
  if (key instanceof String) {
    String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
    if (caseInsensitiveKey != null) {
      return this.targetMap.get(caseInsensitiveKey);
    }
  }
  return null;
}

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

/**
 * Return a copy of the underlying header values as a plain {@link Map} object.
 * <p>This method can be invoked many times, with modifications in between
 * where each new call returns a fresh copy of the current header values.
 */
public Map<String, Object> toMap() {
  return new HashMap<>(this.headers);
}

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

@Test
public void isEmptyMap() {
  assertTrue(isEmpty(Collections.emptyMap()));
  HashMap<String, Object> map = new HashMap<>();
  map.put("foo", 42L);
  assertFalse(isEmpty(map));
}

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

@Override
protected final Map<String, String> create(Entry<String, String>[] entries) {
 HashMap<String, String> map = Maps.newHashMap();
 for (Entry<String, String> entry : entries) {
  map.put(entry.getKey(), entry.getValue());
 }
 return wrap(map);
}

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

static <N, E> DirectedMultiNetworkConnections<N, E> of() {
 return new DirectedMultiNetworkConnections<>(
   new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR),
   new HashMap<E, N>(INNER_CAPACITY, INNER_LOAD_FACTOR),
   0);
}

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

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
  ResourceWebHandler handler = new ResourceWebHandler();
  HashMap<String, ResourceWebHandler> handlerMap = new HashMap<>();
  handlerMap.put("/resources/**", handler);
  SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
  hm.setUrlMap(handlerMap);
  return hm;
}

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

public void testEnumMapWithInitialMap() {
 HashMap<SomeEnum, Integer> original = Maps.newHashMap();
 original.put(SomeEnum.SOME_INSTANCE, 0);
 EnumMap<SomeEnum, Integer> copy = Maps.newEnumMap(original);
 assertEquals(original, copy);
}

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

static <N, V> DirectedGraphConnections<N, V> of() {
 // We store predecessors and successors in the same map, so double the initial capacity.
 int initialCapacity = INNER_CAPACITY * 2;
 return new DirectedGraphConnections<>(
   new HashMap<N, Object>(initialCapacity, INNER_LOAD_FACTOR), 0, 0);
}

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

@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
  ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
  HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<>();
  handlerMap.put("/resources/**", handler);
  SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
  hm.setUrlMap(handlerMap);
  return hm;
}

相关文章