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")); }
@Override public Map<Object, Object> call() throws Exception { return new HashMap<Object, Object>(); } }
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. }
@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"))); }
@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())); }
@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); }
@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; }
@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]")); }
@Override public V put(K key, V value) { if (this.frozen) { throw new UnsupportedOperationException(); } else { return super.put(key, value); } }
@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; }
/** * 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); }
@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); }
@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; }
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); }
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); }
@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; }