congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
Set
Code IndexAdd Tabnine to your IDE (free)

How to use
Set
in
java.util

Best Java code snippets using java.util.Set (Showing top 20 results out of 304,677)

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();
 }
}
java.utilSet

Javadoc

A Set is a data structure which does not allow duplicate elements.

Most used methods

  • add
    Adds the specified element to this set if it is not already present (optional operation). More forma
  • contains
    Returns true if this set contains the specified element. More formally, returns true if and only if
  • iterator
  • size
  • isEmpty
    Returns true if this set contains no elements.
  • addAll
    Adds all of the elements in the specified collection to this set if they're not already present (opt
  • remove
    Removes the specified element from this set if it is present (optional operation). More formally, re
  • toArray
    Returns an array containing all of the elements in this set; the runtime type of the returned array
  • stream
  • clear
    Removes all of the elements from this set (optional operation). The set will be empty after this cal
  • removeAll
    Removes from this set all of its elements that are contained in the specified collection (optional o
  • forEach
  • removeAll,
  • forEach,
  • equals,
  • containsAll,
  • retainAll,
  • hashCode,
  • removeIf,
  • parallelStream,
  • spliterator,
  • of

Popular in Java

  • Reactive rest calls using spring rest template
  • findViewById (Activity)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • getApplicationContext (Context)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • BufferedInputStream (java.io)
    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the i
  • KeyStore (java.security)
    KeyStore is responsible for maintaining cryptographic keys and their owners. The type of the syste
  • Date (java.util)
    A specific moment in time, with millisecond precision. Values typically come from System#currentTime
  • Vector (java.util)
    Vector is an implementation of List, backed by an array and synchronized. All optional operations in
  • JarFile (java.util.jar)
    JarFile is used to read jar entries and their associated data from jar files.
  • Best IntelliJ plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now