java.util.Set.add()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.6k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中java.util.Set.add()方法的一些代码示例,展示了Set.add()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Set.add()方法的具体详情如下:
包路径:java.util.Set
类名称:Set
方法名:add

Set.add介绍

[英]Adds the specified object to this set. The set is not modified if it already contains the object.
[中]将指定的对象添加到此集合。如果集合已包含对象,则不会修改该集合。

代码示例

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"));
}

代码示例来源:origin: google/guava

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;
}

代码示例来源:origin: square/okhttp

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);
 }
}

代码示例来源:origin: spring-projects/spring-framework

private void addToClassHierarchy(int index, Class<?> type, boolean asArray,
    List<Class<?>> hierarchy, Set<Class<?>> visited) {
  if (asArray) {
    type = Array.newInstance(type, 0).getClass();
  }
  if (visited.add(type)) {
    hierarchy.add(index, type);
  }
}

代码示例来源:origin: ctripcorp/apollo

/**
 * 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));
 }
}

代码示例来源:origin: spring-projects/spring-framework

@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));
}

代码示例来源:origin: apache/storm

public static List<String> getRepeat(List<String> list) {
  List<String> rtn = new ArrayList<String>();
  Set<String> idSet = new HashSet<String>();
  for (String id : list) {
    if (idSet.contains(id)) {
      rtn.add(id);
    } else {
      idSet.add(id);
    }
  }
  return rtn;
}

代码示例来源:origin: spring-projects/spring-framework

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");

代码示例来源:origin: google/guava

@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;
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

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);
 }
}

代码示例来源:origin: stanfordnlp/CoreNLP

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;
}

代码示例来源:origin: spring-projects/spring-framework

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;
}

代码示例来源:origin: skylot/jadx

/**
 * @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;
}

代码示例来源:origin: org.testng/testng

private static List<ITestNGMethod> retrieve(Set<String> tracker, Map<String, List<ITestNGMethod>> map, String group) {
 if (tracker.contains(group)) {
  return Collections.EMPTY_LIST;
 }
 tracker.add(group);
 return map.get(group);
}

代码示例来源:origin: hankcs/HanLP

private List<Set<K>> toResult(List<Cluster<K>> clusters_)
{
  List<Set<K>> result = new ArrayList<Set<K>>(clusters_.size());
  for (Cluster<K> c : clusters_)
  {
    Set<K> s = new HashSet<K>();
    for (Document<K> d : c.documents_)
    {
      s.add(d.id_);
    }
    result.add(s);
  }
  return result;
}

代码示例来源:origin: spring-projects/spring-framework

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);

代码示例来源:origin: apache/incubator-dubbo

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;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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);
    }
  }
}

代码示例来源:origin: google/guava

@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();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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);
  }
}

相关文章