java.util.Set类的使用及代码示例

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

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

Set介绍

[英]A Set is a data structure which does not allow duplicate elements.
[中]

代码示例

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<?>> computeKeySetFeatures(Set<Feature<?>> mapFeatures) {
 Set<Feature<?>> keySetFeatures = computeCommonDerivedCollectionFeatures(mapFeatures);
 // TODO(lowasser): make this trigger only if the map is a submap
 // currently, the KeySetGenerator won't work properly for a subset of a keyset of a submap
 keySetFeatures.add(CollectionFeature.SUBSET_VIEW);
 if (mapFeatures.contains(MapFeature.ALLOWS_NULL_KEYS)) {
  keySetFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES);
 } else if (mapFeatures.contains(MapFeature.ALLOWS_NULL_KEY_QUERIES)) {
  keySetFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES);
 }
 return keySetFeatures;
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * Checkout object from pool
 */
public synchronized T checkOut() {
 if (available.isEmpty()) {
  available.add(create());
 }
 T instance = available.iterator().next();
 available.remove(instance);
 inUse.add(instance);
 return instance;
}

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

@Override
public int size() {
 int size = set1.size();
 for (E e : set2) {
  if (!set1.contains(e)) {
   size++;
  }
 }
 return size;
}

代码示例来源:origin: iluwatar/java-design-patterns

public synchronized void checkIn(T instance) {
 inUse.remove(instance);
 available.add(instance);
}

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

private static Set<Feature<?>> computeEntrySetFeatures(Set<Feature<?>> features) {
 Set<Feature<?>> derivedFeatures = new HashSet<>();
 derivedFeatures.addAll(features);
 derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
 derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
 derivedFeatures.remove(CollectionFeature.ALLOWS_NULL_VALUES);
 derivedFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION);
 if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
 }
 return derivedFeatures;
}

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

public void redactHeader(String name) {
 Set<String> newHeadersToRedact = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
 newHeadersToRedact.addAll(headersToRedact);
 newHeadersToRedact.add(name);
 headersToRedact = newHeadersToRedact;
}

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

private Set<Element> createData() {
 Set<Element> set = Sets.newHashSetWithExpectedSize(size);
 while (set.size() < size) {
  set.add(newElement());
 }
 return set;
}

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

private static Set<Feature<?>> computeAsMapFeatures(Set<Feature<?>> multimapFeatures) {
 Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures);
 derivedFeatures.remove(MapFeature.GENERAL_PURPOSE);
 derivedFeatures.remove(MapFeature.SUPPORTS_PUT);
 derivedFeatures.remove(MapFeature.ALLOWS_NULL_VALUES);
 derivedFeatures.add(MapFeature.ALLOWS_NULL_VALUE_QUERIES);
 derivedFeatures.add(MapFeature.REJECTS_DUPLICATES_AT_CREATION);
 if (!derivedFeatures.contains(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
 }
 return derivedFeatures;
}

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

public void testNewIdentityHashSet() {
 Set<Integer> set = Sets.newIdentityHashSet();
 Integer value1 = new Integer(12357);
 Integer value2 = new Integer(12357);
 assertTrue(set.add(value1));
 assertFalse(set.contains(value2));
 assertTrue(set.contains(value1));
 assertTrue(set.add(value2));
 assertEquals(2, set.size());
}

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

static <K, V> Iterator<Entry<K, V>> asMapEntryIterator(
  Set<K> set, final Function<? super K, V> function) {
 return new TransformedIterator<K, Entry<K, V>>(set.iterator()) {
  @Override
  Entry<K, V> transform(final K key) {
   return immutableEntry(key, function.apply(key));
  }
 };
}

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

public void testEmptyRangeSubMultiset(SortedMultiset<E> multiset) {
 assertTrue(multiset.isEmpty());
 assertEquals(0, multiset.size());
 assertEquals(0, multiset.toArray().length);
 assertTrue(multiset.entrySet().isEmpty());
 assertFalse(multiset.iterator().hasNext());
 assertEquals(0, multiset.entrySet().size());
 assertEquals(0, multiset.entrySet().toArray().length);
 assertFalse(multiset.entrySet().iterator().hasNext());
}

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

static Set<Feature<?>> computeElementSetFeatures(Set<Feature<?>> features) {
 Set<Feature<?>> derivedFeatures = new HashSet<>();
 derivedFeatures.addAll(features);
 derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
 derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
 if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
 }
 return derivedFeatures;
}

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

private static void assertEmpty(Set<? extends List<?>> set) {
 assertTrue(set.isEmpty());
 assertEquals(0, set.size());
 assertFalse(set.iterator().hasNext());
}

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

public void testEntrySet() {
 Multiset<Color> ms = EnumMultiset.create(Color.class);
 ms.add(Color.BLUE, 3);
 ms.add(Color.YELLOW, 1);
 ms.add(Color.RED, 2);
 Set<Object> uniqueEntries = Sets.newIdentityHashSet();
 uniqueEntries.addAll(ms.entrySet());
 assertEquals(3, uniqueEntries.size());
}

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

/**
 * Return whether the given profile is active, or if active profiles are empty
 * whether the profile should be active by default.
 * @throws IllegalArgumentException per {@link #validateProfile(String)}
 */
protected boolean isProfileActive(String profile) {
  validateProfile(profile);
  Set<String> currentActiveProfiles = doGetActiveProfiles();
  return (currentActiveProfiles.contains(profile) ||
      (currentActiveProfiles.isEmpty() && doGetDefaultProfiles().contains(profile)));
}

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

@CollectionSize.Require(ONE)
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
public void testEntrySetIteratorRemove() {
 Set<Entry<K, V>> entrySet = getMap().entrySet();
 Iterator<Entry<K, V>> entryItr = entrySet.iterator();
 assertEquals(e0(), entryItr.next());
 entryItr.remove();
 assertTrue(getMap().isEmpty());
 assertFalse(entrySet.contains(e0()));
}

代码示例来源:origin: iluwatar/java-design-patterns

/**
 * return true when globalMutex hold the reference of writerLock
 */
private boolean doesWriterOwnThisLock() {
 return globalMutex.contains(writerLock);
}

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

@Override
public @Nullable E edgeConnectingOrNull(N nodeU, N nodeV) {
 Set<E> edgesConnecting = edgesConnecting(nodeU, nodeV);
 switch (edgesConnecting.size()) {
  case 0:
   return null;
  case 1:
   return edgesConnecting.iterator().next();
  default:
   throw new IllegalArgumentException(String.format(MULTIPLE_EDGES_CONNECTING, nodeU, nodeV));
 }
}

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

private static Set<? extends Annotation> jsonAnnotations(Annotation[] annotations) {
  Set<Annotation> result = null;
  for (Annotation annotation : annotations) {
   if (annotation.annotationType().isAnnotationPresent(JsonQualifier.class)) {
    if (result == null) result = new LinkedHashSet<>();
    result.add(annotation);
   }
  }
  return result != null ? unmodifiableSet(result) : Collections.<Annotation>emptySet();
 }
}

相关文章