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

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

本文整理了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

  1. private void mappingWordsLength(List<String> wordsList) {
  2. Map<Integer, Set<String>> mapping = new HashMap<>();
  3. for (String word : wordsList) {
  4. mapping.computeIfAbsent(word.length(), HashSet::new).add(word);
  5. }
  6. List<Integer> lengths = new LinkedList<>(mapping.keySet());
  7. Collections.sort(lengths);
  8. lengths.forEach(n -> System.out.println(mapping.get(n).size() + " words with " + n + " chars"));
  9. }

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

  1. private static Set<Feature<?>> computeKeySetFeatures(Set<Feature<?>> mapFeatures) {
  2. Set<Feature<?>> keySetFeatures = computeCommonDerivedCollectionFeatures(mapFeatures);
  3. // TODO(lowasser): make this trigger only if the map is a submap
  4. // currently, the KeySetGenerator won't work properly for a subset of a keyset of a submap
  5. keySetFeatures.add(CollectionFeature.SUBSET_VIEW);
  6. if (mapFeatures.contains(MapFeature.ALLOWS_NULL_KEYS)) {
  7. keySetFeatures.add(CollectionFeature.ALLOWS_NULL_VALUES);
  8. } else if (mapFeatures.contains(MapFeature.ALLOWS_NULL_KEY_QUERIES)) {
  9. keySetFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES);
  10. }
  11. return keySetFeatures;
  12. }

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

  1. /**
  2. * Checkout object from pool
  3. */
  4. public synchronized T checkOut() {
  5. if (available.isEmpty()) {
  6. available.add(create());
  7. }
  8. T instance = available.iterator().next();
  9. available.remove(instance);
  10. inUse.add(instance);
  11. return instance;
  12. }

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

  1. @Override
  2. public int size() {
  3. int size = set1.size();
  4. for (E e : set2) {
  5. if (!set1.contains(e)) {
  6. size++;
  7. }
  8. }
  9. return size;
  10. }

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

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

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

  1. private static Set<Feature<?>> computeEntrySetFeatures(Set<Feature<?>> features) {
  2. Set<Feature<?>> derivedFeatures = new HashSet<>();
  3. derivedFeatures.addAll(features);
  4. derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
  5. derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
  6. derivedFeatures.remove(CollectionFeature.ALLOWS_NULL_VALUES);
  7. derivedFeatures.add(CollectionFeature.REJECTS_DUPLICATES_AT_CREATION);
  8. if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  9. derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
  10. }
  11. return derivedFeatures;
  12. }

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

  1. public void redactHeader(String name) {
  2. Set<String> newHeadersToRedact = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
  3. newHeadersToRedact.addAll(headersToRedact);
  4. newHeadersToRedact.add(name);
  5. headersToRedact = newHeadersToRedact;
  6. }

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

  1. private Set<Element> createData() {
  2. Set<Element> set = Sets.newHashSetWithExpectedSize(size);
  3. while (set.size() < size) {
  4. set.add(newElement());
  5. }
  6. return set;
  7. }

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

  1. private static Set<Feature<?>> computeAsMapFeatures(Set<Feature<?>> multimapFeatures) {
  2. Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures);
  3. derivedFeatures.remove(MapFeature.GENERAL_PURPOSE);
  4. derivedFeatures.remove(MapFeature.SUPPORTS_PUT);
  5. derivedFeatures.remove(MapFeature.ALLOWS_NULL_VALUES);
  6. derivedFeatures.add(MapFeature.ALLOWS_NULL_VALUE_QUERIES);
  7. derivedFeatures.add(MapFeature.REJECTS_DUPLICATES_AT_CREATION);
  8. if (!derivedFeatures.contains(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  9. derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
  10. }
  11. return derivedFeatures;
  12. }

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

  1. public void testNewIdentityHashSet() {
  2. Set<Integer> set = Sets.newIdentityHashSet();
  3. Integer value1 = new Integer(12357);
  4. Integer value2 = new Integer(12357);
  5. assertTrue(set.add(value1));
  6. assertFalse(set.contains(value2));
  7. assertTrue(set.contains(value1));
  8. assertTrue(set.add(value2));
  9. assertEquals(2, set.size());
  10. }

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

  1. static <K, V> Iterator<Entry<K, V>> asMapEntryIterator(
  2. Set<K> set, final Function<? super K, V> function) {
  3. return new TransformedIterator<K, Entry<K, V>>(set.iterator()) {
  4. @Override
  5. Entry<K, V> transform(final K key) {
  6. return immutableEntry(key, function.apply(key));
  7. }
  8. };
  9. }

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

  1. public void testEmptyRangeSubMultiset(SortedMultiset<E> multiset) {
  2. assertTrue(multiset.isEmpty());
  3. assertEquals(0, multiset.size());
  4. assertEquals(0, multiset.toArray().length);
  5. assertTrue(multiset.entrySet().isEmpty());
  6. assertFalse(multiset.iterator().hasNext());
  7. assertEquals(0, multiset.entrySet().size());
  8. assertEquals(0, multiset.entrySet().toArray().length);
  9. assertFalse(multiset.entrySet().iterator().hasNext());
  10. }

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

  1. static Set<Feature<?>> computeElementSetFeatures(Set<Feature<?>> features) {
  2. Set<Feature<?>> derivedFeatures = new HashSet<>();
  3. derivedFeatures.addAll(features);
  4. derivedFeatures.remove(CollectionFeature.GENERAL_PURPOSE);
  5. derivedFeatures.remove(CollectionFeature.SUPPORTS_ADD);
  6. if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) {
  7. derivedFeatures.remove(CollectionFeature.SERIALIZABLE);
  8. }
  9. return derivedFeatures;
  10. }

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

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

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

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

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

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

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

  1. @CollectionSize.Require(ONE)
  2. @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
  3. public void testEntrySetIteratorRemove() {
  4. Set<Entry<K, V>> entrySet = getMap().entrySet();
  5. Iterator<Entry<K, V>> entryItr = entrySet.iterator();
  6. assertEquals(e0(), entryItr.next());
  7. entryItr.remove();
  8. assertTrue(getMap().isEmpty());
  9. assertFalse(entrySet.contains(e0()));
  10. }

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

  1. /**
  2. * return true when globalMutex hold the reference of writerLock
  3. */
  4. private boolean doesWriterOwnThisLock() {
  5. return globalMutex.contains(writerLock);
  6. }

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

  1. @Override
  2. public @Nullable E edgeConnectingOrNull(N nodeU, N nodeV) {
  3. Set<E> edgesConnecting = edgesConnecting(nodeU, nodeV);
  4. switch (edgesConnecting.size()) {
  5. case 0:
  6. return null;
  7. case 1:
  8. return edgesConnecting.iterator().next();
  9. default:
  10. throw new IllegalArgumentException(String.format(MULTIPLE_EDGES_CONNECTING, nodeU, nodeV));
  11. }
  12. }

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

  1. private static Set<? extends Annotation> jsonAnnotations(Annotation[] annotations) {
  2. Set<Annotation> result = null;
  3. for (Annotation annotation : annotations) {
  4. if (annotation.annotationType().isAnnotationPresent(JsonQualifier.class)) {
  5. if (result == null) result = new LinkedHashSet<>();
  6. result.add(annotation);
  7. }
  8. }
  9. return result != null ? unmodifiableSet(result) : Collections.<Annotation>emptySet();
  10. }
  11. }

相关文章