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")); }
/** * 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; }
@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; } }
@Override public void testRunFinished(Result result) throws Exception { Thread.setDefaultUncaughtExceptionHandler(oldDefaultUncaughtExceptionHandler); System.err.println("Uninstalled aggressive uncaught exception handler"); synchronized (exceptions) { if (!exceptions.isEmpty()) { throw Throwables.rethrowAsException(exceptions.keySet().iterator().next()); } } } }
@Override public List<URL> lookup(URL url) { List<URL> urls = new ArrayList<>(); Map<String, List<URL>> notifiedUrls = getNotified().get(url); if (notifiedUrls != null && notifiedUrls.size() > 0) { for (List<URL> values : notifiedUrls.values()) { for (URL u : getRegistered()) { if (UrlUtils.isMatch(url, u)) { urls.add(u); for (URL u : getSubscribed().keySet()) { if (UrlUtils.isMatch(url, u)) { urls.add(u);
private Set<String> stringPropertyNames(Properties properties) { //jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历. Map<String, String> h = new HashMap<>(); for (Map.Entry<Object, Object> e : properties.entrySet()) { Object k = e.getKey(); Object v = e.getValue(); if (k instanceof String && v instanceof String) { h.put((String) k, (String) v); } } return h.keySet(); }
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); } }
/** * Return the {@link Method} mapped to the given exception type, or {@code null} if none. */ @Nullable private Method getMappedMethod(Class<? extends Throwable> exceptionType) { List<Class<? extends Throwable>> matches = new ArrayList<>(); for (Class<? extends Throwable> mappedException : this.mappedMethods.keySet()) { if (mappedException.isAssignableFrom(exceptionType)) { matches.add(mappedException); } } if (!matches.isEmpty()) { matches.sort(new ExceptionDepthComparator(exceptionType)); return this.mappedMethods.get(matches.get(0)); } else { return null; } }
@Override public Map<String, List<String>> getDirectoryPathsOnTiers() { Map<String, List<String>> pathsOnTiers = new HashMap<>(); for (Pair<String, String> tierPath : mCapacityBytesOnDirs.keySet()) { String tier = tierPath.getFirst(); if (pathsOnTiers.get(tier) == null) { pathsOnTiers.put(tier, new ArrayList<String>()); } pathsOnTiers.get(tier).add(tierPath.getSecond()); } return pathsOnTiers; }
static <K extends Enum<K>> Class<K> inferKeyType(Map<K, ?> map) { if (map instanceof EnumBiMap) { return ((EnumBiMap<K, ?>) map).keyType(); } if (map instanceof EnumHashBiMap) { return ((EnumHashBiMap<K, ?>) map).keyType(); } checkArgument(!map.isEmpty()); return map.keySet().iterator().next().getDeclaringClass(); }
@Override public List<URL> lookup(URL url) { List<URL> urls = new ArrayList<>(); Map<String, List<URL>> notifiedUrls = getNotified().get(url); if (notifiedUrls != null && notifiedUrls.size() > 0) { for (List<URL> values : notifiedUrls.values()) { for (URL u : getRegistered()) { if (UrlUtils.isMatch(url, u)) { urls.add(u); for (URL u : getSubscribed().keySet()) { if (UrlUtils.isMatch(url, u)) { urls.add(u);
@Override public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException { Map<String, Object> results = new LinkedHashMap<>(); for (String beanName : this.beans.keySet()) { if (findAnnotationOnBean(beanName, annotationType) != null) { results.put(beanName, getBean(beanName)); } } return results; }