Class Sets
Set instances. Also see this class's counterparts
Lists, Maps and Queues.
See the Guava User Guide article on Sets.
- Since:
- 2.0
- Author:
- Kevin Bourrillion, Jared Levy, Chris Povirk
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAn unmodifiable view of a set which may be backed by other sets; this view will change as the backing sets do. -
Method Summary
Modifier and TypeMethodDescriptioncomplementOf(Collection<E> collection) Creates anEnumSetconsisting of all enum values that are not in the specified collection.complementOf(Collection<E> collection, Class<E> type) Creates anEnumSetconsisting of all enum values that are not in the specified collection.static <E> Set<E>Creates a thread-safe set backed by a hash map.static <E> CopyOnWriteArraySet<E>Creates an emptyCopyOnWriteArraySetinstance.static <E> HashSet<E>Creates a mutable, initially emptyHashSetinstance.static <E> HashSet<E>newHashSet(E... elements) Creates a mutableHashSetinstance initially containing the given elements.static <E> HashSet<E>newHashSet(Iterator<? extends E> elements) Creates a mutableHashSetinstance containing the given elements.static <E> HashSet<E>newHashSetWithExpectedSize(int expectedSize) Returns a new hash set using the smallest initial table size that can holdexpectedSizeelements without resizing.static <E> Set<E>Creates an emptySetthat uses identity to determine equality.static <E> LinkedHashSet<E>Creates a mutable, emptyLinkedHashSetinstance.static <E> LinkedHashSet<E>newLinkedHashSetWithExpectedSize(int expectedSize) Creates aLinkedHashSetinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth.static <E> Set<E>newSetFromMap(Map<E, Boolean> map) Deprecated.static <E extends Comparable>
TreeSet<E>Creates a mutable, emptyTreeSetinstance sorted by the natural sort ordering of its elements.static <E> TreeSet<E>newTreeSet(Comparator<? super E> comparator) Creates a mutable, emptyTreeSetinstance with the given comparator.
-
Method Details
-
newHashSet
Creates a mutable, initially emptyHashSetinstance.Note: if mutability is not required, use
ImmutableSet.of()instead. IfEis anEnumtype, useEnumSet.noneOf(java.lang.Class<E>)instead. Otherwise, strongly consider using aLinkedHashSetinstead, at the cost of increased memory footprint, to get deterministic iteration behavior.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
HashSetconstructor directly, taking advantage of the new "diamond" syntax. -
newHashSet
Creates a mutableHashSetinstance initially containing the given elements.Note: if elements are non-null and won't be added or removed after this point, use
ImmutableSet.of()orImmutableSet.copyOf(Object[])instead. IfEis anEnumtype, useEnumSet.of(Enum, Enum[])instead. Otherwise, strongly consider using aLinkedHashSetinstead, at the cost of increased memory footprint, to get deterministic iteration behavior.This method is just a small convenience, either for
newHashSet(asList(...)), or for creating an empty set then callingCollections.addAll(java.util.Collection<? super T>, T...). This method is not actually very useful and will likely be deprecated in the future. -
newHashSet
Creates a mutableHashSetinstance containing the given elements. A very thin convenience for creating an empty set and then callingIterators.addAll(java.util.Collection<T>, java.util.Iterator<? extends T>).Note: if mutability is not required and the elements are non-null, use
ImmutableSet.copyOf(Iterator)instead.Note: if
Eis anEnumtype, you should create anEnumSetinstead.Overall, this method is not very useful and will likely be deprecated in the future.
-
newHashSetWithExpectedSize
Returns a new hash set using the smallest initial table size that can holdexpectedSizeelements without resizing. Note that this is not whatHashSet(int)does, but it is what most users want and expect it to do.This behavior can't be broadly guaranteed, but has been tested with OpenJDK 1.7 and 1.8.
- Parameters:
expectedSize- the number of elements you expect to add to the returned set- Returns:
- a new, empty hash set with enough capacity to hold
expectedSizeelements without resizing - Throws:
IllegalArgumentException- ifexpectedSizeis negative
-
newConcurrentHashSet
Creates a thread-safe set backed by a hash map. The set is backed by aConcurrentHashMapinstance, and thus carries the same concurrency guarantees.Unlike
HashSet, this class does NOT allownullto be used as an element. The set is serializable.- Returns:
- a new, empty thread-safe
Set - Since:
- 15.0
-
newLinkedHashSet
Creates a mutable, emptyLinkedHashSetinstance.Note: if mutability is not required, use
ImmutableSet.of()instead.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
LinkedHashSetconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
LinkedHashSet
-
newLinkedHashSetWithExpectedSize
Creates aLinkedHashSetinstance, with a high enough "initial capacity" that it should holdexpectedSizeelements without growth. This behavior cannot be broadly guaranteed, but it is observed to be true for OpenJDK 1.7. It also can't be guaranteed that the method isn't inadvertently oversizing the returned set.- Parameters:
expectedSize- the number of elements you expect to add to the returned set- Returns:
- a new, empty
LinkedHashSetwith enough capacity to holdexpectedSizeelements without resizing - Throws:
IllegalArgumentException- ifexpectedSizeis negative- Since:
- 11.0
-
newTreeSet
Creates a mutable, emptyTreeSetinstance sorted by the natural sort ordering of its elements.Note: if mutability is not required, use
ImmutableSortedSet#of()instead.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
TreeSetconstructor directly, taking advantage of the new "diamond" syntax.- Returns:
- a new, empty
TreeSet
-
newTreeSet
Creates a mutable, emptyTreeSetinstance with the given comparator.Note: if mutability is not required, use
ImmutableSortedSet.orderedBy(comparator).build()instead.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
TreeSetconstructor directly, taking advantage of the new "diamond" syntax. One caveat to this is that theTreeSetconstructor uses a nullComparatorto mean "natural ordering," whereas this factory rejects null. Clean your code accordingly.- Parameters:
comparator- the comparator to use to sort the set- Returns:
- a new, empty
TreeSet - Throws:
NullPointerException- ifcomparatoris null
-
newIdentityHashSet
Creates an emptySetthat uses identity to determine equality. It compares object references, instead of callingequals, to determine whether a provided object matches an element in the set. For example,containsreturnsfalsewhen passed an object that equals a set member, but isn't the same instance. This behavior is similar to the wayIdentityHashMaphandles key lookups.- Since:
- 8.0
-
newCopyOnWriteArraySet
Creates an emptyCopyOnWriteArraySetinstance.Note: if you need an immutable empty
Set, useCollections.emptySet()instead.- Returns:
- a new, empty
CopyOnWriteArraySet - Since:
- 12.0
-
complementOf
Creates anEnumSetconsisting of all enum values that are not in the specified collection. If the collection is anEnumSet, this method has the same behavior asEnumSet.complementOf(java.util.EnumSet<E>). Otherwise, the specified collection must contain at least one element, in order to determine the element type. If the collection could be empty, usecomplementOf(Collection, Class)instead of this method.- Parameters:
collection- the collection whose complement should be stored in the enum set- Returns:
- a new, modifiable
EnumSetcontaining all values of the enum that aren't present in the given collection - Throws:
IllegalArgumentException- ifcollectionis not anEnumSetinstance and contains no elements
-
complementOf
Creates anEnumSetconsisting of all enum values that are not in the specified collection. This is equivalent toEnumSet.complementOf(java.util.EnumSet<E>), but can act on any input collection, as long as the elements are of enum type.- Parameters:
collection- the collection whose complement should be stored in theEnumSettype- the type of the elements in the set- Returns:
- a new, modifiable
EnumSetinitially containing all the values of the enum not present in the given collection
-
newSetFromMap
Deprecated.Returns a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides aSetimplementation corresponding to anyMapimplementation. There is no need to use this method on aMapimplementation that already has a correspondingSetimplementation (such asHashMaporTreeMap).Each method invocation on the set returned by this method results in exactly one method invocation on the backing map or its
keySetview, with one exception. TheaddAllmethod is implemented as a sequence ofputinvocations on the backing map.The specified map must be empty at the time this method is invoked, and should not be accessed directly after this method returns. These conditions are ensured if the map is created empty, passed directly to this method, and no reference to the map is retained, as illustrated in the following code fragment:
Set<Object> identityHashSet = Sets.newSetFromMap( new IdentityHashMap<Object, Boolean>());The returned set is serializable if the backing map is.
- Parameters:
map- the backing map- Returns:
- the set backed by the map
- Throws:
IllegalArgumentException- ifmapis not empty
-
Collections.newSetFromMap(java.util.Map<E, java.lang.Boolean>)instead.