|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.google.common.collect.Sets
public final class Sets
Provides static methods for creating mutable Set instances easily and
other static methods for working with Sets.
You can replace code like:
Set<String> set = new HashSet<String>();
Collections.addAll(set, "foo", "bar", "baz");
with just:
Set<String> set = newHashSet("foo", "bar", "baz");
You can also create an empty Set, or populate your new Set
using any array, Iterator or Iterable.
See also this class's counterparts Lists and Maps.
WARNING: These factories do not support the full variety of tuning parameters available in the collection constructors. Use them only for collections which will always remain small, or for which the cost of future growth operations is not a concern.
| Method Summary | ||
|---|---|---|
static
|
complementOf(Collection<E> collection)
Creates an enum set with the given element type, initially containing all the elements of this type that are not contained in the specified set. |
|
static
|
complementOf(Collection<E> collection,
Class<E> type)
Creates an enum set with the given element type, initially containing all the elements of this type that are not contained in the specified set. |
|
static
|
emptySortedSet()
Returns the empty sorted set (immutable). |
|
static
|
immutableEnumSet(E anElement,
E... otherElements)
Returns an immutable Set instance containing the given elements of
an enumerated type. |
|
static
|
immutableSet()
Optimization of #immutableSet for zero arguments. |
|
static
|
immutableSet(Collection<E> collection)
Returns an immutable Set instance containing the elements in the provided set. |
|
static
|
immutableSet(E... elements)
Returns an immutable Set instance containing the given elements. |
|
static
|
immutableSet(E element)
Optimization of #immutableSet for one argument. |
|
static
|
immutableSortedSet()
Returns an empty sorted set. |
|
static
|
immutableSortedSet(Comparator<? super E> comparator)
Returns an empty sorted set having the given Comparator. |
|
static
|
immutableSortedSet(Comparator<? super E> comparator,
E... elements)
Returns an immutable SortedSet instance containing the given elements sorted by the given Comparator. |
|
static
|
immutableSortedSet(Comparator<? super E> comparator,
E element)
Returns a sorted set having only the given element and the given Comparator. |
|
static
|
immutableSortedSet(E... elements)
Returns an immutable SortedSet instance containing the given elements sorted by their natural ordering. |
|
static
|
immutableSortedSet(E element)
Returns a sorted set having only the given element. |
|
static
|
newConcurrentHashSet()
Creates a thread-safe set backed by a hash map. |
|
static
|
newConcurrentHashSet(E... elements)
Creates a thread-safe set backed by a hash map, and containing the given elements. |
|
static
|
newConcurrentHashSet(Iterable<? extends E> elements)
Creates a thread-safe set backed by a hash map, and containing the given elements. |
|
static
|
newConcurrentHashSet(Iterator<? extends E> elements)
Creates a thread-safe set backed by a hash map, and containing the given elements. |
|
static
|
newHashSet()
Creates an empty HashSet instance. |
|
static
|
newHashSet(E... elements)
Creates a HashSet instance containing the given elements. |
|
static
|
newHashSet(Iterable<? extends E> elements)
Creates a HashSet instance containing the given elements. |
|
static
|
newHashSet(Iterator<? extends E> elements)
Creates a HashSet instance containing the given elements. |
|
static
|
newLinkedHashSet()
Creates an empty LinkedHashSet instance. |
|
static
|
newLinkedHashSet(E... elements)
Creates a LinkedHashSet instance containing the given elements. |
|
static
|
newLinkedHashSet(Iterable<? extends E> elements)
Creates a LinkedHashSet instance containing the given elements. |
|
static
|
newLinkedHashSet(Iterator<? extends E> elements)
Creates a LinkedHashSet instance containing the given elements. |
|
static
|
newSetFromMap(Map<E,Boolean> map)
Returns a set backed by the specified map. |
|
static
|
newSortedArraySet()
Creates an empty SortedArraySet instance, with an initial capacity
of zero. |
|
static
|
newSortedArraySet(Comparator<? super E> comparator)
Creates an empty SortedArraySet instance, with an initial capacity
of zero. |
|
static
|
newSortedArraySet(Comparator<? super E> comparator,
E... elements)
Creates a SortedArraySet instance containing the given elements. |
|
static
|
newSortedArraySet(E... elements)
Creates a SortedArraySet instance containing the given elements. |
|
static
|
newSortedArraySet(Iterable<? extends E> elements)
Creates a SortedArraySet instance containing the given elements. |
|
static
|
newSortedArraySet(Iterable<? extends E> elements,
Comparator<? super E> comparator)
Creates a SortedArraySet instance containing the given elements. |
|
static
|
newTreeSet()
Creates a TreeSet instance using the default Comparator. |
|
static
|
newTreeSet(Comparator<? super E> comparator)
Creates a TreeSet instance using the given Comparator. |
|
static
|
newTreeSet(Comparator<? super E> comparator,
E... elements)
Creates a TreeSet instance using the given Comparator and
containing the given elements. |
|
static
|
newTreeSet(Comparator<? super E> comparator,
Iterable<? extends E> elements)
Creates a TreeSet instance using the given Comparator and
containing the given elements. |
|
static
|
newTreeSet(Comparator<? super E> comparator,
Iterator<? extends E> elements)
Creates a TreeSet instance using the given Comparator and
containing the given elements. |
|
static
|
newTreeSet(E... elements)
Creates a TreeSet instance using the default Comparator and
containing the given elements. |
|
static
|
newTreeSet(Iterable<? extends E> elements)
Creates a TreeSet instance using the default Comparator and
containing the given elements. |
|
static
|
newTreeSet(Iterator<? extends E> elements)
Creates a TreeSet instance using the default Comparator and
containing the given elements. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <E extends Comparable> SortedSet<E> immutableSortedSet(E... elements)
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
SortedSet<Base> set = Sets.immutableSortedSet(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
SortedSet<Base> set = Sets.<Base>immutableSortedSet(sub1,
sub2);
elements - the elements that the set should contain
SortedSet instance containing those elements,
minus duplicates
public static <E> SortedSet<E> immutableSortedSet(@Nullable
Comparator<? super E> comparator,
E... elements)
Comparator.
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
SortedSet<Base> set = Sets.immutableSortedSet(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
SortedSet<Base> set = Sets.<Base>immutableSortedSet(sub1,
sub2);
comparator - the comparator to sort the elements byelements - the elements that the set should contain
SortedSet instance containing those elements,
minus duplicatespublic static <E extends Comparable> SortedSet<E> immutableSortedSet()
immutableSortedSet(Comparable...) for zero arguments.
public static <E> SortedSet<E> immutableSortedSet(@Nullable
Comparator<? super E> comparator)
Comparator. This is an
optimization of immutableSortedSet(Comparator,Object...) for zero
arguments.
comparator - the comparator for the empty set
public static <E extends Comparable> SortedSet<E> immutableSortedSet(E element)
immutableSortedSet(Comparable...) for one argument.
element - the lone element to be in the returned set
public static <E> SortedSet<E> immutableSortedSet(@Nullable
Comparator<? super E> comparator,
@Nullable
E element)
Comparator. This is an optimization of immutableSortedSet(Comparator,Object...) for one argument.
comparator - the comparator for the one-element setelement - the lone element to be in the returned set
public static <E> Set<E> immutableSet(E... elements)
Unlike an unmodifiable set such as that returned by Collections.unmodifiableSet(), which provides a read-only view of an
underlying set which may itself be mutable, an immutable set makes a
copy of the original set or collection, so that changes to the original are
not reflected in the immutable set.
Immutability has two important advantages over unmodifiability. First,
it allows the hash code to be computed once and cached, rather than
computed every time it is needed, which takes O(n) time for a set of n
elements. Second, it prevents any inadvertent modification of the
value of the set. This is critical, for example, if the set is an element
of a HashSet or a key in a HashMap.
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
Set<Base> set = Sets.immutableSet(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
Set<Base> set = Sets.<Base>immutableSet(sub1, sub2);
Note: If <E> is an enum type, use immutableEnumSet(E, E...) instead.
elements - the elements that the set should contain
Set instance containing those elements, minus
duplicatespublic static <E> Set<E> immutableSet()
#immutableSet for zero arguments.
Collections.emptySet()
public static <E> Set<E> immutableSet(@Nullable
E element)
#immutableSet for one argument.
element - the lone element to be in the returned set
Collections.singleton(T)public static <E> Set<E> immutableSet(Collection<E> collection)
immutableSet(Object...) for details.
collection - a collection containing the elements to be in the
returned set
Set instance containing those elements
public static <E extends Enum<E>> Set<E> immutableEnumSet(E anElement,
E... otherElements)
Set instance containing the given elements of
an enumerated type. Internally this set will be backed by an EnumSet.
anElement - one of the elements the set should containotherElements - the rest of the elements the set should contain
Set instance containing these elements, minus
duplicatespublic static <E> HashSet<E> newHashSet()
HashSet instance.
Note: if E is an Enum type, use EnumSet.noneOf(java.lang.Class instead.
Note: if you only need an immutable empty Set, use Collections.emptySet() instead.
HashSetpublic static <E> HashSet<E> newHashSet(E... elements)
HashSet instance containing the given elements.
Note: if E is an Enum type, use EnumSet.of(Enum, Enum...) instead.
Note: if it is an immutable Set you seek, you should use immutableSet(Object...).
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
Set<Base> set = Sets.newHashSet(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
Set<Base> set = Sets.<Base>newHashSet(sub1, sub2);
elements - the elements that the set should contain
HashSet containing those elements (minus
duplicates)public static <E> HashSet<E> newHashSet(Iterable<? extends E> elements)
HashSet instance containing the given elements.
Note: if E is an Enum type, and elements
is a Collection, use EnumSet.copyOf(Collection) instead.
elements - the elements that the set should contain
HashSet containing those elements (minus
duplicates)public static <E> HashSet<E> newHashSet(Iterator<? extends E> elements)
HashSet instance containing the given elements.
Note: if E is an Enum type, you should manually
create an EnumSet instead.
elements - the elements that the set should contain
HashSet containing those elements (minus
duplicates)public static <E> Set<E> newConcurrentHashSet()
ConcurrentHashMap instance, and thus carries the same concurrency
guarantees.
Unlike HashSet, this class does NOT allow null to be
used as an element.
Setpublic static <E> Set<E> newConcurrentHashSet(E... elements)
ConcurrentHashMap instance, and
thus carries the same concurrency guarantees.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
elements - the elements that the set should contain
Set containing those elements
(minus duplicates)public static <E> Set<E> newConcurrentHashSet(Iterable<? extends E> elements)
ConcurrentHashMap instance, and
thus carries the same concurrency guarantees.
elements - the elements that the set should contain
Set containing those elements
(minus duplicates)public static <E> Set<E> newConcurrentHashSet(Iterator<? extends E> elements)
ConcurrentHashMap instance, and
thus carries the same concurrency guarantees.
elements - the elements that the set should contain
Set containing those elements
(minus duplicates)public static <E> LinkedHashSet<E> newLinkedHashSet()
LinkedHashSet instance.
LinkedHashSetpublic static <E> LinkedHashSet<E> newLinkedHashSet(E... elements)
LinkedHashSet instance containing the given elements.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
elements - the elements that the set should contain, in order
LinkedHashSet containing those elements
(minus duplicates)public static <E> LinkedHashSet<E> newLinkedHashSet(Iterable<? extends E> elements)
LinkedHashSet instance containing the given elements.
elements - the elements that the set should contain, in order
LinkedHashSet containing those elements
(minus duplicates)public static <E> LinkedHashSet<E> newLinkedHashSet(Iterator<? extends E> elements)
LinkedHashSet instance containing the given elements.
elements - the elements that the set should contain, in order
LinkedHashSet containing those elements
(minus duplicates)public static <E extends Comparable> TreeSet<E> newTreeSet()
TreeSet instance using the default Comparator.
Note: If E is an Enum type, and you don't require
the set to implement SortedSet (only ordered iteration), use EnumSet.noneOf(java.lang.Class instead.
TreeSetpublic static <E extends Comparable> TreeSet<E> newTreeSet(E... elements)
TreeSet instance using the default Comparator and
containing the given elements.
Note: If E is an Enum type, and you don't
require the set to implement SortedSet (only ordered iteration),
use EnumSet.of(Enum, Enum...) instead.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
elements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)public static <E extends Comparable> TreeSet<E> newTreeSet(Iterable<? extends E> elements)
TreeSet instance using the default Comparator and
containing the given elements.
Note: If E is an Enum type, and you don't require
the set to implement SortedSet (only ordered iteration), use EnumSet.copyOf(Collection) instead.
elements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)public static <E extends Comparable> TreeSet<E> newTreeSet(Iterator<? extends E> elements)
TreeSet instance using the default Comparator and
containing the given elements.
Note: if E is an Enum type, and you don't
require the set to implement SortedSet (only ordered iteration),
you should manually create an EnumSet instead.
elements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)
public static <E> TreeSet<E> newTreeSet(@Nullable
Comparator<? super E> comparator)
TreeSet instance using the given Comparator.
comparator - the comparator to use to sort the set
TreeSet
public static <E> TreeSet<E> newTreeSet(@Nullable
Comparator<? super E> comparator,
E... elements)
TreeSet instance using the given Comparator and
containing the given elements.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
comparator - the comparator to use to sort the setelements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)
public static <E> TreeSet<E> newTreeSet(@Nullable
Comparator<? super E> comparator,
Iterable<? extends E> elements)
TreeSet instance using the given Comparator and
containing the given elements.
comparator - the comparator to use to sort the setelements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)
public static <E> TreeSet<E> newTreeSet(@Nullable
Comparator<? super E> comparator,
Iterator<? extends E> elements)
TreeSet instance using the given Comparator and
containing the given elements.
comparator - the comparator to use to sort the setelements - the elements that the set should contain
TreeSet containing those elements (minus
duplicates)public static <E extends Comparable> SortedArraySet<E> newSortedArraySet()
SortedArraySet instance, with an initial capacity
of zero.
TODO: change the initial capacity to the traditional default of ten.
SortedArraySetpublic static <E> SortedArraySet<E> newSortedArraySet(Comparator<? super E> comparator)
SortedArraySet instance, with an initial capacity
of zero.
TODO: change the initial capacity to the traditional default of ten.
comparator - the comparator to use
SortedArraySetpublic static <E extends Comparable> SortedArraySet<E> newSortedArraySet(Iterable<? extends E> elements)
SortedArraySet instance containing the given elements.
TODO: change this method to preserve the ordering of the specified
iterable if it is an instance of SortedSet.
elements - the elements that the set should contain
SortedArraySet containing those elements
(minus duplicates)
public static <E> SortedArraySet<E> newSortedArraySet(Iterable<? extends E> elements,
Comparator<? super E> comparator)
SortedArraySet instance containing the given elements.
TODO: change the initial capacity to the traditional default of ten, if the iterable is not a collection.
elements - the elements that the set should containcomparator - the comparator to use
SortedArraySet containing those elements
(minus duplicates)
public static <E> SortedArraySet<E> newSortedArraySet(Comparator<? super E> comparator,
E... elements)
SortedArraySet instance containing the given elements.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
comparator - the comparator to useelements - the elements that the set should contain
SortedArraySet containing those elements
(minus duplicates)public static <E extends Comparable> SortedArraySet<E> newSortedArraySet(E... elements)
SortedArraySet instance containing the given elements.
Please see the notice in newHashSet(Object...) about a relevant
javac bug.
elements - the elements that the set should contain
SortedArraySet containing those elements
(minus duplicates)public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collection)
EnumSet instance, this
method behaves identically to EnumSet.complementOf(java.util.EnumSet) . Otherwise, the
specified collection must contain at least one element (in order to
determine the new enum set's element type). If the collection could
possibly be empty, use complementOf(Collection,Class) instead of
this method.
collection - the collection from whose complement to initialize the
enum set
IllegalArgumentException - if set is not an EnumSet
instance and contains no elements
public static <E extends Enum<E>> EnumSet<E> complementOf(Collection<E> collection,
Class<E> type)
EnumSet.complementOf(java.util.EnumSet) , but can act on any
type of set, so long as the elements are of enum type.
collection - the collection from whose complement to initialize this
enum settype - the type of the elements in the set
public static <E> Set<E> newSetFromMap(Map<E,Boolean> map)
Set implementation
corresponding to any Map implementation. There is no need to use
this method on a map implementation that already has a corresponding set
implementation (such as HashMap or TreeMap).
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<Foo> identityHashSet = Sets.newSetFromMap(
new IdentityHashMap<Foo, Boolean>());
public static <E> SortedSet<E> emptySortedSet()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||