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")); }
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; }
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()); }); }
private static Map<ByteString, Integer> nameToFirstIndex() { Map<ByteString, Integer> result = new LinkedHashMap<>(STATIC_HEADER_TABLE.length); for (int i = 0; i < STATIC_HEADER_TABLE.length; i++) { if (!result.containsKey(STATIC_HEADER_TABLE[i].name)) { result.put(STATIC_HEADER_TABLE[i].name, i); } } return Collections.unmodifiableMap(result); }
static void tryPutIntoPool(boolean purgeEnabled, ScheduledExecutorService exec) { if (purgeEnabled && exec instanceof ScheduledThreadPoolExecutor) { ScheduledThreadPoolExecutor e = (ScheduledThreadPoolExecutor) exec; POOLS.put(e, exec); } }
@SuppressWarnings("unchecked") @Override public void removePredecessor(N node) { Object previousValue = adjacentNodeValues.get(node); if (previousValue == PRED) { adjacentNodeValues.remove(node); checkNonNegative(--predecessorCount); } else if (previousValue instanceof PredAndSucc) { adjacentNodeValues.put((N) node, ((PredAndSucc) previousValue).successorValue); checkNonNegative(--predecessorCount); } }
private static void insertIntoReplica(Map<Integer, AtomicInteger> replica, int newValue) { if (replica.containsKey(newValue)) { replica.get(newValue).incrementAndGet(); } else { replica.put(newValue, new AtomicInteger(1)); } }
@GwtIncompatible // SerializableTester public void testViewSerialization() { Map<String, Integer> map = ImmutableMap.of("one", 1, "two", 2, "three", 3); LenientSerializableTester.reserializeAndAssertLenient(map.entrySet()); LenientSerializableTester.reserializeAndAssertLenient(map.keySet()); Collection<Integer> reserializedValues = reserialize(map.values()); assertEquals(Lists.newArrayList(map.values()), Lists.newArrayList(reserializedValues)); assertTrue(reserializedValues instanceof ImmutableCollection); }
@Override public V apply(@Nullable K key) { V result = map.get(key); return (result != null || map.containsKey(key)) ? result : defaultValue; }
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { for (String key : attrsToValidate.keySet()) { assertTrue("Model should contain attribute named " + key, model.containsKey(key)); assertEquals(attrsToValidate.get(key), model.get(key)); validatedAttrCount++; } } };
@Override public Optional<Customer> getById(final int id) { return Optional.ofNullable(idToCustomer.get(id)); }
public void testRowKeyMapHeadMap() { sortedTable = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c'); Map<String, Map<Integer, Character>> map = sortedTable.rowMap().headMap("cat"); assertEquals(1, map.size()); assertEquals(ImmutableMap.of(1, 'b'), map.get("bar")); map.clear(); assertTrue(map.isEmpty()); assertEquals(Collections.singleton("foo"), sortedTable.rowKeySet()); }
public void testAsMap() { Set<String> strings = ImmutableSet.of("one", "two", "three"); Map<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map); assertEquals(Integer.valueOf(5), map.get("three")); assertNull(map.get("five")); assertThat(map.entrySet()) .containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5)) .inOrder(); }