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")); }
private static Set<Feature<?>> computeValuesSetFeatures(Set<Feature<?>> mapFeatures) { Set<Feature<?>> valuesCollectionFeatures = computeCommonDerivedCollectionFeatures(mapFeatures); valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES); if (mapFeatures.contains(MapFeature.ALLOWS_NULL_VALUES)) { valuesCollectionFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES); } valuesCollectionFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION); return valuesCollectionFeatures; }
public BasicCertificateChainCleaner(X509Certificate... caCerts) { subjectToCaCerts = new LinkedHashMap<>(); for (X509Certificate caCert : caCerts) { X500Principal subject = caCert.getSubjectX500Principal(); Set<X509Certificate> subjectCaCerts = subjectToCaCerts.get(subject); if (subjectCaCerts == null) { subjectCaCerts = new LinkedHashSet<>(1); subjectToCaCerts.put(subject, subjectCaCerts); } subjectCaCerts.add(caCert); } }
/** * Delete existed property */ public void deleteProperty(String namespace, String someKey) { if (deletedKeysOfNamespace.containsKey(namespace)) { deletedKeysOfNamespace.get(namespace).add(someKey); } else { deletedKeysOfNamespace.put(namespace, ImmutableSet.of(someKey)); } }
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @Test public void setCollectionPropertyWithArrayValue() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); Collection<String> coll = new HashSet<>(); coll.add("coll1"); accessor.setPropertyValue("collection", coll.toArray()); List<String> set = new LinkedList<>(); set.add("set1"); accessor.setPropertyValue("set", set.toArray()); List<String> sortedSet = new ArrayList<>(); sortedSet.add("sortedSet1"); accessor.setPropertyValue("sortedSet", sortedSet.toArray()); Set<String> list = new HashSet<>(); list.add("list1"); accessor.setPropertyValue("list", list.toArray()); assertEquals(1, target.getCollection().size()); assertTrue(target.getCollection().containsAll(coll)); assertEquals(1, target.getSet().size()); assertTrue(target.getSet().containsAll(set)); assertEquals(1, target.getSortedSet().size()); assertTrue(target.getSortedSet().containsAll(sortedSet)); assertEquals(1, target.getList().size()); assertTrue(target.getList().containsAll(list)); }
Enumeration<String> parameterEnum = request.getParameterNames(); while (parameterEnum.hasMoreElements()) { parameterNames.add(parameterEnum.nextElement()); assertEquals(3, parameterNames.size()); assertTrue(parameterNames.contains("field3")); assertTrue(parameterNames.contains("field4")); assertTrue(parameterNames.contains("getField")); assertEquals("value3", request.getParameter("field3")); List<String> parameterValues = Arrays.asList(request.getParameterValues("field3")); assertEquals(1, parameterValues.size()); assertTrue(parameterValues.contains("value3")); assertEquals("value4", request.getParameter("field4")); parameterValues = Arrays.asList(request.getParameterValues("field4")); assertEquals(2, parameterValues.size()); assertTrue(parameterValues.contains("value4")); assertTrue(parameterValues.contains("value5")); for (Object o : request.getParameterMap().keySet()) { String key = (String) o; parameterMapKeys.add(key); parameterMapValues.add(request.getParameterMap().get(key)); assertEquals(3, parameterMapKeys.size()); assertEquals(3, parameterMapValues.size()); int field3Index = parameterMapKeys.indexOf("field3");
@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; } }
protected void updateKeys(Set<Iterable<K>> keys, List<K> prefix) { if (children != null) { for (Entry<K, TrieMap<K, V>> kTrieMapEntry : children.entrySet()) { List<K> p = new ArrayList<>(prefix.size() + 1); p.addAll(prefix); p.add(kTrieMapEntry.getKey()); kTrieMapEntry.getValue().updateKeys(keys, p); } } if (value != null) { keys.add(prefix); } }
private static List<Sieve.MentionData> eliminateDuplicates(List<Sieve.MentionData> mentionCandidates) { List<Sieve.MentionData> newList = new ArrayList<>(); Set<String> seenText = new HashSet<>(); for(int i = 0; i < mentionCandidates.size(); i++) { Sieve.MentionData mentionCandidate = mentionCandidates.get(i); String text = mentionCandidate.text; if(!seenText.contains(text) || mentionCandidate.type.equals("Pronoun")) newList.add(mentionCandidate); seenText.add(text); } return newList; }
public Collection<SourceClass> getAnnotationAttributes(String annType, String attribute) throws IOException { Map<String, Object> annotationAttributes = this.metadata.getAnnotationAttributes(annType, true); if (annotationAttributes == null || !annotationAttributes.containsKey(attribute)) { return Collections.emptySet(); } String[] classNames = (String[]) annotationAttributes.get(attribute); Set<SourceClass> result = new LinkedHashSet<>(); for (String className : classNames) { result.add(getRelated(className)); } return result; }
/** * @return true if this value is duplicated */ public boolean put(Object value, FieldNode fld) { FieldNode prev = values.put(value, fld); if (prev != null) { values.remove(value); duplicates.add(value); return true; } if (duplicates.contains(value)) { values.remove(value); return true; } return false; }
if (valuesAlreadyReplaced.contains(attributeOverrideName)) { continue; targetAttributeNames.add(attributeOverrideName); valuesAlreadyReplaced.add(attributeOverrideName); List<String> aliases = AnnotationUtils.getAttributeAliasMap(targetAnnotationType).get(attributeOverrideName); if (aliases != null) { for (String alias : aliases) { if (!valuesAlreadyReplaced.contains(alias)) { targetAttributeNames.add(alias); valuesAlreadyReplaced.add(alias);
private Set<String> resolvePackagesToScan(Set<String> packagesToScan) { Set<String> resolvedPackagesToScan = new LinkedHashSet<String>(packagesToScan.size()); for (String packageToScan : packagesToScan) { if (StringUtils.hasText(packageToScan)) { String resolvedPackageToScan = environment.resolvePlaceholders(packageToScan.trim()); resolvedPackagesToScan.add(resolvedPackageToScan); } } return resolvedPackagesToScan; }
/** * Add the given singleton factory for building the specified singleton * if necessary. * <p>To be called for eager registration of singletons, e.g. to be able to * resolve circular references. * @param beanName the name of the bean * @param singletonFactory the factory for the singleton object */ protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized (this.singletonObjects) { if (!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }
@Override public Map<K, Collection<V>> create(Object... elements) { Set<K> keySet = new HashSet<>(); List<Entry<K, V>> builder = new ArrayList<>(); for (Object o : elements) { Entry<K, Collection<V>> entry = (Entry<K, Collection<V>>) o; keySet.add(entry.getKey()); for (V v : entry.getValue()) { builder.add(mapEntry(entry.getKey(), v)); } } checkArgument(keySet.size() == elements.length, "Duplicate keys"); return multimapGenerator.create(builder.toArray()).asMap(); }
/** * Add the given singleton object to the singleton cache of this factory. * <p>To be called for eager registration of singletons. * @param beanName the name of the bean * @param singletonObject the singleton object */ protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }