Top Description Inners Constructors Methods
com.google.common.collect

public final Class Sets

extends Object
Class Inheritance
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.collect.CollectPreconditions.checkNonnegative

Static utility methods pertaining to Set instances. Also see this class's counterparts Lists, Maps and Queues.

See the Guava User Guide article on Sets.

Authors
Kevin Bourrillion, Jared Levy, Chris Povirk
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class
private static class
private static class
private static class
pack-priv abstract static class
Sets.ImprovedAbstractSet<E extends @Nullable Object>

AbstractSet substitute without the potentially-quadratic removeAll implementation.

private static class
public abstract static class
Sets.SetView<E extends @Nullable Object>

An unmodifiable view of a set which may be backed by other sets; this view will change as the backing sets do.

private static class
pack-priv static class

Constructor Summary

AccessConstructor and Description
private
Sets()

Method Summary

Modifier and TypeMethod and Description
public static <
any common base class shared by all axes (often just Object)
B
>
Set<List<B>>

Returns:

the Cartesian product, as an immutable set containing immutable lists
cartesianProduct
(List<? extends Set<? extends B>>
the sets to choose elements from, in the order that the elements chosen from those sets should appear in the resulting lists
sets
)

Returns every possible list that can be formed by choosing one element from each of the given sets in order; the "n-ary Cartesian product" of the sets.

public static <
any common base class shared by all axes (often just Object)
B
>
Set<List<B>>

Returns:

the Cartesian product, as an immutable set containing immutable lists
cartesianProduct
(Set<? extends B>...
the sets to choose elements from, in the order that the elements chosen from those sets should appear in the resulting lists
sets
)

Returns every possible list that can be formed by choosing one element from each of the given sets in order; the "n-ary Cartesian product" of the sets.

public static <E> Set<Set<E>>

Returns:

the set of all combinations of size elements from set
combinations
(Set<E>
the set of elements to take combinations of
set
,
final int
the number of elements per combination
size
)

Returns the set of all subsets of set of size size.

public static <E extends Enum<E>> EnumSet<E>

Returns:

a new, modifiable EnumSet containing all values of the enum that aren't present in the given collection
complementOf
(Collection<E>
the collection whose complement should be stored in the enum set
collection
)

Creates an EnumSet consisting of all enum values that are not in the specified collection.

public static <E extends Enum<E>> EnumSet<E>

Returns:

a new, modifiable EnumSet initially containing all the values of the enum not present in the given collection
complementOf
(Collection<E>
the collection whose complement should be stored in the EnumSet
collection
,
Class<E>
the type of the elements in the set
type
)

Creates an EnumSet consisting of all enum values that are not in the specified collection.

public static <E extends @Nullable Object> Sets.SetView<E>
difference(final Set<E> set1, final Set<?> set2)

Returns an unmodifiable view of the difference of two sets.

pack-priv static boolean
equalsImpl(Set<?> s, Object object)

An implementation for Set#equals(Object).

public static <E extends @Nullable Object> Set<E>
filter(Set<E> unfiltered, Predicate<? super E> predicate)

Returns the elements of unfiltered that satisfy a predicate.

public static <E extends @Nullable Object> SortedSet<E>
filter(SortedSet<E> unfiltered, Predicate<? super E> predicate)

Returns the elements of a SortedSet, unfiltered, that satisfy a predicate.

public static <E extends @Nullable Object> NavigableSet<E>
filter(NavigableSet<E> unfiltered, Predicate<? super E> predicate)

Returns the elements of a NavigableSet, unfiltered, that satisfy a predicate.

pack-priv static int
hashCodeImpl(Set<?> s)

An implementation for Set#hashCode().

public static <E extends Enum<E>> ImmutableSet<E>

Returns:

an immutable set containing those elements, minus duplicates
immutableEnumSet
(E
one of the elements the set should contain
anElement
,
E...
the rest of the elements the set should contain
otherElements
)

Returns an immutable set instance containing the given enum elements.

public static <E extends Enum<E>> ImmutableSet<E>

Returns:

an immutable set containing those elements, minus duplicates
immutableEnumSet
(Iterable<E>
the elements, all of the same enum type, that the set should contain
elements
)

Returns an immutable set instance containing the given enum elements.

public static <E extends @Nullable Object> Sets.SetView<E>
intersection(final Set<E> set1, final Set<?> set2)

Returns an unmodifiable view of the intersection of two sets.

private static <E extends Enum<E>> EnumSet<E>
makeComplementByHand(Collection<E> collection, Class<E> type)

public static <E> Set<E>

Returns:

a new, empty thread-safe Set
newConcurrentHashSet
()

Creates a thread-safe set backed by a hash map.

public static <E> Set<E>

Returns:

a new thread-safe set containing those elements (minus duplicates)
newConcurrentHashSet
(Iterable<? extends E>
the elements that the set should contain
elements
)

Creates a thread-safe set backed by a hash map and containing the given elements.

public static <E extends @Nullable Object> CopyOnWriteArraySet<E>

Returns:

a new, empty CopyOnWriteArraySet
newCopyOnWriteArraySet
()

Creates an empty CopyOnWriteArraySet instance.

public static <E extends @Nullable Object> CopyOnWriteArraySet<E>

Returns:

a new CopyOnWriteArraySet containing those elements
newCopyOnWriteArraySet
(Iterable<? extends E>
the elements that the set should contain, in order
elements
)

Creates a CopyOnWriteArraySet instance containing the given elements.

public static <E extends Enum<E>> EnumSet<E>
newEnumSet(Iterable<E> iterable, Class<E> elementType)

Returns a new, mutable EnumSet instance containing the given elements in their natural order.

public static <E extends @Nullable Object> HashSet<E>
newHashSet()

Creates a mutable, initially empty HashSet instance.

public static <E extends @Nullable Object> HashSet<E>
newHashSet(E... elements)

Creates a mutable HashSet instance initially containing the given elements.

public static <E extends @Nullable Object> HashSet<E>
newHashSet(Iterable<? extends E> elements)

Creates a mutable HashSet instance containing the given elements.

public static <E extends @Nullable Object> HashSet<E>
newHashSet(Iterator<? extends E> elements)

Creates a mutable HashSet instance containing the given elements.

public static <E extends @Nullable Object> HashSet<E>

Returns:

a new, empty hash set with enough capacity to hold expectedSize elements without resizing
newHashSetWithExpectedSize
(int
the number of elements you expect to add to the returned set
expectedSize
)

Returns a new hash set using the smallest initial table size that can hold expectedSize elements without resizing.

public static <E extends @Nullable Object> Set<E>
newIdentityHashSet()

Creates an empty Set that uses identity to determine equality.

public static <E extends @Nullable Object> LinkedHashSet<E>

Returns:

a new, empty LinkedHashSet
newLinkedHashSet
()

Creates a mutable, empty LinkedHashSet instance.

public static <E extends @Nullable Object> LinkedHashSet<E>

Returns:

a new LinkedHashSet containing those elements (minus duplicates)
newLinkedHashSet
(Iterable<? extends E>
the elements that the set should contain, in order
elements
)

Creates a mutable LinkedHashSet instance containing the given elements in order.

public static <E extends @Nullable Object> LinkedHashSet<E>

Returns:

a new, empty LinkedHashSet with enough capacity to hold expectedSize elements without resizing
newLinkedHashSetWithExpectedSize
(int
the number of elements you expect to add to the returned set
expectedSize
)

Creates a LinkedHashSet instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth.

public static <E extends @Nullable Object> Set<E>

Returns:

the set backed by the map
newSetFromMap
(Map<E, Boolean>
the backing map
map
)

Deprecated Use Collections#newSetFromMap instead.
Returns a set backed by the specified map.
public static <E extends Comparable<T>> TreeSet<E>

Returns:

a new, empty TreeSet
newTreeSet
()

Creates a mutable, empty TreeSet instance sorted by the natural sort ordering of its elements.

public static <E extends Comparable<T>> TreeSet<E>

Returns:

a new TreeSet containing those elements (minus duplicates)
newTreeSet
(Iterable<? extends E>
the elements that the set should contain
elements
)

Creates a mutable TreeSet instance containing the given elements sorted by their natural ordering.

public static <E extends @Nullable Object> TreeSet<E>

Returns:

a new, empty TreeSet
newTreeSet
(Comparator<? super E>
the comparator to use to sort the set
comparator
)

Creates a mutable, empty TreeSet instance with the given comparator.

public static <E> Set<Set<E>>

Returns:

the power set, as an immutable set of immutable sets
powerSet
(Set<E>
the set of elements to construct a power set from
set
)

Returns the set of all possible subsets of set.

pack-priv static boolean
removeAllImpl(Set<?> set, Iterator<?> iterator)

Remove each element in an iterable from a set.

pack-priv static boolean
removeAllImpl(Set<?> set, Collection<?> collection)

public static <K extends Comparable<? super K>> NavigableSet<K>
subSet(NavigableSet<K> set, Range<K> range)

Returns a view of the portion of set whose elements are contained by range.

public static <E extends @Nullable Object> Sets.SetView<E>
symmetricDifference(final Set<? extends E> set1, final Set<? extends E> set2)

Returns an unmodifiable view of the symmetric difference of two sets.

public static <E extends @Nullable Object> NavigableSet<E>

Returns:

a synchronized view of the specified navigable set.
synchronizedNavigableSet
(NavigableSet<E>
the navigable set to be "wrapped" in a synchronized navigable set.
navigableSet
)

Returns a synchronized (thread-safe) navigable set backed by the specified navigable set.

public static <E extends Enum<E>> Collector<E, ?, ImmutableSet<E>>
toImmutableEnumSet()

Returns a Collector that accumulates the input elements into a new ImmutableSet with an implementation specialized for enums.

public static <E extends @Nullable Object> Sets.SetView<E>
union(final Set<? extends E> set1, final Set<? extends E> set2)

Returns an unmodifiable view of the union of two sets.

public static <E extends @Nullable Object> NavigableSet<E>

Returns:

an unmodifiable view of the specified navigable set
unmodifiableNavigableSet
(NavigableSet<E>
the navigable set for which an unmodifiable view is to be returned
set
)

Returns an unmodifiable view of the specified navigable set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait