public final class Sets extends Object
"Unmodifiable Set Static Factory Methods" in the Set
interface.
The Sets.of and Sets.copyOf
static factory methods provide a convenient way to create unmodifiable sets.
The Set instances created by these methods have the following
characteristics:
UnsupportedOperationException to be thrown.
However, if the contained elements are themselves mutable, this may cause the
Set to behave inconsistently or its contents to appear to change.
null elements. Attempts to create them with
null elements result in NullPointerException.
IllegalArgumentException.
==), identity hash code, and synchronization) are unreliable and
should be avoided.
| Modifier and Type | Method and Description |
|---|---|
static <E> Set<E> |
copyOf(Collection<? extends E> coll)
Returns an unmodifiable Set containing the elements
of the given Collection.
|
static <E> Set<E> |
of()
Returns an unmodifiable set containing zero elements.
|
static <E> Set<E> |
of(E... elements)
Returns an unmodifiable set containing an arbitrary number of elements.
|
static <E> Set<E> |
of(E e1)
Returns an unmodifiable set containing one element.
|
static <E> Set<E> |
of(E e1,
E e2)
Returns an unmodifiable set containing two elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3)
Returns an unmodifiable set containing three elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4)
Returns an unmodifiable set containing four elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5)
Returns an unmodifiable set containing five elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6)
Returns an unmodifiable set containing six elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7)
Returns an unmodifiable set containing seven elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7,
E e8)
Returns an unmodifiable set containing eight elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7,
E e8,
E e9)
Returns an unmodifiable set containing nine elements.
|
static <E> Set<E> |
of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7,
E e8,
E e9,
E e10)
Returns an unmodifiable set containing ten elements.
|
public static <E> Set<E> of()
E - the Set's element typeSetpublic static <E> Set<E> of(E e1)
E - the Set's element typee1 - the single elementSet containing the specified elementNullPointerException - if the element is nullpublic static <E> Set<E> of(E e1, E e2)
E - the Set's element typee1 - the first elemente2 - the second elementSet containing the specified elementsIllegalArgumentException - if the elements are duplicatesNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elemente9 - the ninth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)
E - the Set's element typee1 - the first elemente2 - the second elemente3 - the third elemente4 - the fourth elemente5 - the fifth elemente6 - the sixth elemente7 - the seventh elemente8 - the eighth elemente9 - the ninth elemente10 - the tenth elementSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is nullpublic static <E> Set<E> of(E... elements)
API Note:
This method also accepts a single array as an argument. The element type of
the resulting set will be the component type of the array, and the size of
the set will be equal to the length of the array. To create a set with
a single element that is an array, do the following:
String[] array = ... ;
Set<String[]> list = Sets.<String[]>of(array);
This will cause the Sets.of(E) method
to be invoked instead.E - the Set's element typeelements - the elements to be contained in the setSet containing the specified elementsIllegalArgumentException - if there are any duplicate elementsNullPointerException - if an element is null or if the array is nullpublic static <E> Set<E> copyOf(Collection<? extends E> coll)
Implementation Note: If the given Collection is an unmodifiable Set, calling copyOf will generally not create a copy.
E - the Set's element typecoll - the collection from which elements are drawn, must be non-nullSet containing the elements of the given CollectionNullPointerException - if coll is null, or if it contains any nullsCopyright © 2018. All rights reserved.