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; }
/** * 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; }
void addContributor(String owner, String repo, String name, int contributions) { Map<String, List<Contributor>> repoContributors = ownerRepoContributors.get(owner); if (repoContributors == null) { repoContributors = new LinkedHashMap<>(); ownerRepoContributors.put(owner, repoContributors); } List<Contributor> contributors = repoContributors.get(repo); if (contributors == null) { contributors = new ArrayList<>(); repoContributors.put(repo, contributors); } contributors.add(new Contributor(name, contributions)); } }
static String extractStatusLine(Map<String, List<String>> javaResponseHeaders) throws ProtocolException { List<String> values = javaResponseHeaders.get(null); if (values == null || values.size() == 0) { // The status line is missing. This suggests a badly behaving cache. throw new ProtocolException( "CacheResponse is missing a \'null\' header containing the status line. Headers=" + javaResponseHeaders); } return values.get(0); }
private byte[] encodeHeaderKey(String input, boolean escape) { String inputToUse = (escape ? escape(input) : input); if (this.headerKeyAccessCache.containsKey(inputToUse)) { return this.headerKeyAccessCache.get(inputToUse); } synchronized (this.headerKeyUpdateCache) { byte[] bytes = this.headerKeyUpdateCache.get(inputToUse); if (bytes == null) { bytes = inputToUse.getBytes(StandardCharsets.UTF_8); this.headerKeyAccessCache.put(inputToUse, bytes); this.headerKeyUpdateCache.put(inputToUse, bytes); } return bytes; } }
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); } }
private List<ContentGroup> getContentsForCharacter(FontCharacter character) { if (contentsForCharacter.containsKey(character)) { return contentsForCharacter.get(character); } List<ShapeGroup> shapes = character.getShapes(); int size = shapes.size(); List<ContentGroup> contents = new ArrayList<>(size); for (int i = 0; i < size; i++) { ShapeGroup sg = shapes.get(i); contents.add(new ContentGroup(lottieDrawable, this, sg)); } contentsForCharacter.put(character, contents); return contents; }
private List<AbstractProject> get(Map<AbstractProject, List<DependencyGroup>> map, AbstractProject src, boolean up) { List<DependencyGroup> v = map.get(src); if(v==null) return Collections.emptyList(); List<AbstractProject> result = new ArrayList<AbstractProject>(v.size()); for (DependencyGroup d : v) result.add(up ? d.getUpstreamProject() : d.getDownstreamProject()); return result; }
public static String getVerticalFormattedOutput(List<Map<String, Object>> content, String[] titleFields) { List<String[]> printContents = new ArrayList<String[]>(); int maxCol = content.size() > MAX_COL ? MAX_COL : content.size(); for (String titleField : titleFields) { String[] row = new String[maxCol + 1]; row[0] = titleField; for (int j = 0; j < maxCol; j++) { Map<String, Object> sqlStat = content.get(j); Object value = sqlStat.get(titleField); row[j + 1] = handleAndConvert(value, titleField); } printContents.add(row); } return TableFormatter.format(printContents); }
/** * Add an option argument for the given option name and add the given value to the * list of values associated with this option (of which there may be zero or more). * The given value may be {@code null}, indicating that the option was specified * without an associated value (e.g. "--foo" vs. "--foo=bar"). */ public void addOptionArg(String optionName, @Nullable String optionValue) { if (!this.optionArgs.containsKey(optionName)) { this.optionArgs.put(optionName, new ArrayList<>()); } if (optionValue != null) { this.optionArgs.get(optionName).add(optionValue); } }
@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; } }
@Test public void testGenericTypeNestingListOfMapOfInteger() throws Exception { List<Map<String, String>> list = new LinkedList<>(); Map<String, String> map = new HashMap<>(); map.put("testKey", "5"); list.add(map); NestedGenericCollectionBean gb = new NestedGenericCollectionBean(); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfMapOfInteger", list); Object obj = gb.getListOfMapOfInteger().get(0).get("testKey"); assertTrue(obj instanceof Integer); assertEquals(5, ((Integer) obj).intValue()); }
@Implementation(minSdk = JELLY_BEAN_MR2) protected Account[] getAccountsByTypeForPackage(String type, String packageName) { List<Account> result = new ArrayList<>(); Account[] accountsByType = getAccountsByType(type); for (Account account : accountsByType) { if (packageVisibileAccounts.containsKey(account) && packageVisibileAccounts.get(account).contains(packageName)) { result.add(account); } } return result.toArray(new Account[result.size()]); }
@Nullable private Object getNativeHeaderValue(Message<?> message, String name) { Map<String, List<String>> nativeHeaders = getNativeHeaders(message); if (name.startsWith("nativeHeaders.")) { name = name.substring("nativeHeaders.".length()); } if (nativeHeaders == null || !nativeHeaders.containsKey(name)) { return null; } List<?> nativeHeaderValues = nativeHeaders.get(name); return (nativeHeaderValues.size() == 1 ? nativeHeaderValues.get(0) : nativeHeaderValues); }
@Test public void testGenericListOfMaps() throws MalformedURLException { GenericBean<String> gb = new GenericBean<>(); List<Map<Integer, Long>> list = new LinkedList<>(); list.add(new HashMap<>()); gb.setListOfMaps(list); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("listOfMaps[0][10]", new Long(5)); assertEquals(new Long(5), bw.getPropertyValue("listOfMaps[0][10]")); assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10)); }
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()); }); }
@Override protected List<Entry> buildFeedEntries(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { List<Entry> entries = new ArrayList<>(); for (String name : model.keySet()) { Entry entry = new Entry(); entry.setTitle(name); Content content = new Content(); content.setValue((String) model.get(name)); entry.setSummary(content); entries.add(entry); } return entries; } }