< prev index next >

## src/java.base/share/classes/java/util/Set.java

Print this page

    rev 47953 : 8177290: add copy factory methods for unmodifiable List, Set, Map
    8184690: add Collectors for collecting into unmodifiable List, Set, and Map
    Reviewed-by: alanb, briangoetz, dholmes, jrose, rriggs, scolebourne

      46  * <p>Note: Great care must be exercised if mutable objects are used as set
      47  * elements.  The behavior of a set is not specified if the value of an object
      48  * is changed in a manner that affects {@code equals} comparisons while the
      49  * object is an element in the set.  A special case of this prohibition is
      50  * that it is not permissible for a set to contain itself as an element.
      51  *
      52  * <p>Some set implementations have restrictions on the elements that
      53  * they may contain.  For example, some implementations prohibit null elements,
      54  * and some have restrictions on the types of their elements.  Attempting to
      55  * add an ineligible element throws an unchecked exception, typically
      56  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
      57  * to query the presence of an ineligible element may throw an exception,
      58  * or it may simply return false; some implementations will exhibit the former
      59  * behavior and some will exhibit the latter.  More generally, attempting an
      60  * operation on an ineligible element whose completion would not result in
      61  * the insertion of an ineligible element into the set may throw an
      62  * exception or it may succeed, at the option of the implementation.
      63  * Such exceptions are marked as "optional" in the specification for this
      64  * interface.
      65  *
      66  * <h2><a id="immutable">Immutable Set Static Factory Methods</a></h2>
      67  * <p>The {@link Set#of(Object...) Set.of()} static factory methods
      68  * provide a convenient way to create immutable sets. The {@code Set}

      69  * instances created by these methods have the following characteristics:
      70  *
      71  * <ul>
      72  * <li>They are <em>structurally immutable</em>. Elements cannot be added or
      73  * removed. Calling any mutator method will always cause
      74  * {@code UnsupportedOperationException} to be thrown.
      75  * However, if the contained elements are themselves mutable, this may cause the
      76  * Set to behave inconsistently or its contents to appear to change.
      77  * <li>They disallow {@code null} elements. Attempts to create them with
      78  * {@code null} elements result in {@code NullPointerException}.
      79  * <li>They are serializable if all elements are serializable.
      80  * <li>They reject duplicate elements at creation time. Duplicate elements
      81  * passed to a static factory method result in {@code IllegalArgumentException}.
      82  * <li>The iteration order of set elements is unspecified and is subject to change.
      83  * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
      84  * Callers should make no assumptions about the identity of the returned instances.
      85  * Factories are free to create new instances or reuse existing ones. Therefore,
      86  * identity-sensitive operations on these instances (reference equality ({@code ==}),
      87  * identity hash code, and synchronization) are unreliable and should be avoided.
      88  * <li>They are serialized as specified on the
      89  * <a href="{@docRoot}/serialized-form.html#java.util.CollSer">Serialized Form</a>
      90  * page.
      91  * </ul>
      92  *
      93  * <p>This interface is a member of the
      94  * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
     422      * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
     423      * from the set's {@code Iterator}.  The spliterator inherits the
     424      * <em>fail-fast</em> properties of the set's iterator.
     425      * <p>
     426      * The created {@code Spliterator} additionally reports
     427      * {@link Spliterator#SIZED}.
     428      *
     429      * @implNote
     430      * The created {@code Spliterator} additionally reports
     431      * {@link Spliterator#SUBSIZED}.
     432      *
     433      * @return a {@code Spliterator} over the elements in this set
     434      * @since 1.8
     435      */
     436     @Override
     437     default Spliterator<E> spliterator() {
     438         return Spliterators.spliterator(this, Spliterator.DISTINCT);
     439     }
     440
     441     /**
     442      * Returns an immutable set containing zero elements.
     443      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     444      *
     445      * @param <E> the {@code Set}'s element type
     446      * @return an empty {@code Set}
     447      *
     448      * @since 9
     449      */
     450     static <E> Set<E> of() {
     451         return ImmutableCollections.Set0.instance();
     452     }
     453
     454     /**
     455      * Returns an immutable set containing one element.
     456      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     457      *
     458      * @param <E> the {@code Set}'s element type
     459      * @param e1 the single element
     460      * @return a {@code Set} containing the specified element
     461      * @throws NullPointerException if the element is {@code null}
     462      *
     463      * @since 9
     464      */
     465     static <E> Set<E> of(E e1) {
     466         return new ImmutableCollections.Set1<>(e1);
     467     }
     468
     469     /**
     470      * Returns an immutable set containing two elements.
     471      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     472      *
     473      * @param <E> the {@code Set}'s element type
     474      * @param e1 the first element
     475      * @param e2 the second element
     476      * @return a {@code Set} containing the specified elements
     477      * @throws IllegalArgumentException if the elements are duplicates
     478      * @throws NullPointerException if an element is {@code null}
     479      *
     480      * @since 9
     481      */
     482     static <E> Set<E> of(E e1, E e2) {
     483         return new ImmutableCollections.Set2<>(e1, e2);
     484     }
     485
     486     /**
     487      * Returns an immutable set containing three elements.
     488      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     489      *
     490      * @param <E> the {@code Set}'s element type
     491      * @param e1 the first element
     492      * @param e2 the second element
     493      * @param e3 the third element
     494      * @return a {@code Set} containing the specified elements
     495      * @throws IllegalArgumentException if there are any duplicate elements
     496      * @throws NullPointerException if an element is {@code null}
     497      *
     498      * @since 9
     499      */
     500     static <E> Set<E> of(E e1, E e2, E e3) {
     501         return new ImmutableCollections.SetN<>(e1, e2, e3);
     502     }
     503
     504     /**
     505      * Returns an immutable set containing four elements.
     506      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     507      *
     508      * @param <E> the {@code Set}'s element type
     509      * @param e1 the first element
     510      * @param e2 the second element
     511      * @param e3 the third element
     512      * @param e4 the fourth element
     513      * @return a {@code Set} containing the specified elements
     514      * @throws IllegalArgumentException if there are any duplicate elements
     515      * @throws NullPointerException if an element is {@code null}
     516      *
     517      * @since 9
     518      */
     519     static <E> Set<E> of(E e1, E e2, E e3, E e4) {
     520         return new ImmutableCollections.SetN<>(e1, e2, e3, e4);
     521     }
     522
     523     /**
     524      * Returns an immutable set containing five elements.
     525      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     526      *
     527      * @param <E> the {@code Set}'s element type
     528      * @param e1 the first element
     529      * @param e2 the second element
     530      * @param e3 the third element
     531      * @param e4 the fourth element
     532      * @param e5 the fifth element
     533      * @return a {@code Set} containing the specified elements
     534      * @throws IllegalArgumentException if there are any duplicate elements
     535      * @throws NullPointerException if an element is {@code null}
     536      *
     537      * @since 9
     538      */
     539     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) {
     540         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5);
     541     }
     542
     543     /**
     544      * Returns an immutable set containing six elements.
     545      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     546      *
     547      * @param <E> the {@code Set}'s element type
     548      * @param e1 the first element
     549      * @param e2 the second element
     550      * @param e3 the third element
     551      * @param e4 the fourth element
     552      * @param e5 the fifth element
     553      * @param e6 the sixth element
     554      * @return a {@code Set} containing the specified elements
     555      * @throws IllegalArgumentException if there are any duplicate elements
     556      * @throws NullPointerException if an element is {@code null}
     557      *
     558      * @since 9
     559      */
     560     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
     561         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     562                                                e6);
     563     }
     564
     565     /**
     566      * Returns an immutable set containing seven elements.
     567      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     568      *
     569      * @param <E> the {@code Set}'s element type
     570      * @param e1 the first element
     571      * @param e2 the second element
     572      * @param e3 the third element
     573      * @param e4 the fourth element
     574      * @param e5 the fifth element
     575      * @param e6 the sixth element
     576      * @param e7 the seventh element
     577      * @return a {@code Set} containing the specified elements
     578      * @throws IllegalArgumentException if there are any duplicate elements
     579      * @throws NullPointerException if an element is {@code null}
     580      *
     581      * @since 9
     582      */
     583     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
     584         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     585                                                e6, e7);
     586     }
     587
     588     /**
     589      * Returns an immutable set containing eight elements.
     590      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     591      *
     592      * @param <E> the {@code Set}'s element type
     593      * @param e1 the first element
     594      * @param e2 the second element
     595      * @param e3 the third element
     596      * @param e4 the fourth element
     597      * @param e5 the fifth element
     598      * @param e6 the sixth element
     599      * @param e7 the seventh element
     600      * @param e8 the eighth element
     601      * @return a {@code Set} containing the specified elements
     602      * @throws IllegalArgumentException if there are any duplicate elements
     603      * @throws NullPointerException if an element is {@code null}
     604      *
     605      * @since 9
     606      */
     607     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
     608         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     609                                                e6, e7, e8);
     610     }
     611
     612     /**
     613      * Returns an immutable set containing nine elements.
     614      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     615      *
     616      * @param <E> the {@code Set}'s element type
     617      * @param e1 the first element
     618      * @param e2 the second element
     619      * @param e3 the third element
     620      * @param e4 the fourth element
     621      * @param e5 the fifth element
     622      * @param e6 the sixth element
     623      * @param e7 the seventh element
     624      * @param e8 the eighth element
     625      * @param e9 the ninth element
     626      * @return a {@code Set} containing the specified elements
     627      * @throws IllegalArgumentException if there are any duplicate elements
     628      * @throws NullPointerException if an element is {@code null}
     629      *
     630      * @since 9
     631      */
     632     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
     633         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     634                                                e6, e7, e8, e9);
     635     }
     636
     637     /**
     638      * Returns an immutable set containing ten elements.
     639      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     640      *
     641      * @param <E> the {@code Set}'s element type
     642      * @param e1 the first element
     643      * @param e2 the second element
     644      * @param e3 the third element
     645      * @param e4 the fourth element
     646      * @param e5 the fifth element
     647      * @param e6 the sixth element
     648      * @param e7 the seventh element
     649      * @param e8 the eighth element
     650      * @param e9 the ninth element
     651      * @param e10 the tenth element
     652      * @return a {@code Set} containing the specified elements
     653      * @throws IllegalArgumentException if there are any duplicate elements
     654      * @throws NullPointerException if an element is {@code null}
     655      *
     656      * @since 9
     657      */
     658     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) {
     659         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     660                                                e6, e7, e8, e9, e10);
     661     }
     662
     663     /**
     664      * Returns an immutable set containing an arbitrary number of elements.
     665      * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
     666      *
     667      * @apiNote
     668      * This method also accepts a single array as an argument. The element type of
     669      * the resulting set will be the component type of the array, and the size of
     670      * the set will be equal to the length of the array. To create a set with
     671      * a single element that is an array, do the following:
     672      *
     673      * <pre>{@code
     674      *     String[] array = ... ;
     675      *     Set<String[]> list = Set.<String[]>of(array);
     676      * }</pre>
     677      *
     678      * This will cause the {@link Set#of(Object) Set.of(E)} method
     679      * to be invoked instead.
     680      *
     681      * @param <E> the {@code Set}'s element type
     682      * @param elements the elements to be contained in the set
     683      * @return a {@code Set} containing the specified elements
     684      * @throws IllegalArgumentException if there are any duplicate elements
     685      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
     686      *
     687      * @since 9
     688      */
     689     @SafeVarargs
     690     @SuppressWarnings("varargs")
     691     static <E> Set<E> of(E... elements) {
     692         switch (elements.length) { // implicit null check of elements
     693             case 0:
     694                 return ImmutableCollections.Set0.instance();
     695             case 1:
     696                 return new ImmutableCollections.Set1<>(elements[0]);
     697             case 2:
     698                 return new ImmutableCollections.Set2<>(elements[0], elements[1]);
     699             default:
     700                 return new ImmutableCollections.SetN<>(elements);
     701         }
     702     }

     703 }
      46  * <p>Note: Great care must be exercised if mutable objects are used as set
      47  * elements.  The behavior of a set is not specified if the value of an object
      48  * is changed in a manner that affects {@code equals} comparisons while the
      49  * object is an element in the set.  A special case of this prohibition is
      50  * that it is not permissible for a set to contain itself as an element.
      51  *
      52  * <p>Some set implementations have restrictions on the elements that
      53  * they may contain.  For example, some implementations prohibit null elements,
      54  * and some have restrictions on the types of their elements.  Attempting to
      55  * add an ineligible element throws an unchecked exception, typically
      56  * {@code NullPointerException} or {@code ClassCastException}.  Attempting
      57  * to query the presence of an ineligible element may throw an exception,
      58  * or it may simply return false; some implementations will exhibit the former
      59  * behavior and some will exhibit the latter.  More generally, attempting an
      60  * operation on an ineligible element whose completion would not result in
      61  * the insertion of an ineligible element into the set may throw an
      62  * exception or it may succeed, at the option of the implementation.
      63  * Such exceptions are marked as "optional" in the specification for this
      64  * interface.
      65  *
      66  * <h2><a id="unmodifiable">Unmodifiable Sets</a></h2>
      67  * <p>The {@link Set#of(Object...) Set.of} and
      68  * {@link Set#copyOf Set.copyOf} static factory methods
      69  * provide a convenient way to create unmodifiable sets. The {@code Set}
      70  * instances created by these methods have the following characteristics:
      71  *
      72  * <ul>
      73  * <li>They are <a href="Collection.html#unmodifiable"><i>unmodifiable</i></a>. Elements cannot
      74  * be added or removed. Calling any mutator method on the Set
      75  * will always cause {@code UnsupportedOperationException} to be thrown.
      76  * However, if the contained elements are themselves mutable, this may cause the
      77  * Set to behave inconsistently or its contents to appear to change.
      78  * <li>They disallow {@code null} elements. Attempts to create them with
      79  * {@code null} elements result in {@code NullPointerException}.
      80  * <li>They are serializable if all elements are serializable.
      81  * <li>They reject duplicate elements at creation time. Duplicate elements
      82  * passed to a static factory method result in {@code IllegalArgumentException}.
      83  * <li>The iteration order of set elements is unspecified and is subject to change.
      84  * <li>They are <a href="../lang/doc-files/ValueBased.html">value-based</a>.
      85  * Callers should make no assumptions about the identity of the returned instances.
      86  * Factories are free to create new instances or reuse existing ones. Therefore,
      87  * identity-sensitive operations on these instances (reference equality ({@code ==}),
      88  * identity hash code, and synchronization) are unreliable and should be avoided.
      89  * <li>They are serialized as specified on the
      90  * <a href="{@docRoot}/serialized-form.html#java.util.CollSer">Serialized Form</a>
      91  * page.
      92  * </ul>
      93  *
      94  * <p>This interface is a member of the
      95  * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
     423      * <em><a href="Spliterator.html#binding">late-binding</a></em> spliterator
     424      * from the set's {@code Iterator}.  The spliterator inherits the
     425      * <em>fail-fast</em> properties of the set's iterator.
     426      * <p>
     427      * The created {@code Spliterator} additionally reports
     428      * {@link Spliterator#SIZED}.
     429      *
     430      * @implNote
     431      * The created {@code Spliterator} additionally reports
     432      * {@link Spliterator#SUBSIZED}.
     433      *
     434      * @return a {@code Spliterator} over the elements in this set
     435      * @since 1.8
     436      */
     437     @Override
     438     default Spliterator<E> spliterator() {
     439         return Spliterators.spliterator(this, Spliterator.DISTINCT);
     440     }
     441
     442     /**
     443      * Returns an unmodifiable set containing zero elements.
     444      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     445      *
     446      * @param <E> the {@code Set}'s element type
     447      * @return an empty {@code Set}
     448      *
     449      * @since 9
     450      */
     451     static <E> Set<E> of() {
     452         return ImmutableCollections.Set0.instance();
     453     }
     454
     455     /**
     456      * Returns an unmodifiable set containing one element.
     457      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     458      *
     459      * @param <E> the {@code Set}'s element type
     460      * @param e1 the single element
     461      * @return a {@code Set} containing the specified element
     462      * @throws NullPointerException if the element is {@code null}
     463      *
     464      * @since 9
     465      */
     466     static <E> Set<E> of(E e1) {
     467         return new ImmutableCollections.Set1<>(e1);
     468     }
     469
     470     /**
     471      * Returns an unmodifiable set containing two elements.
     472      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     473      *
     474      * @param <E> the {@code Set}'s element type
     475      * @param e1 the first element
     476      * @param e2 the second element
     477      * @return a {@code Set} containing the specified elements
     478      * @throws IllegalArgumentException if the elements are duplicates
     479      * @throws NullPointerException if an element is {@code null}
     480      *
     481      * @since 9
     482      */
     483     static <E> Set<E> of(E e1, E e2) {
     484         return new ImmutableCollections.Set2<>(e1, e2);
     485     }
     486
     487     /**
     488      * Returns an unmodifiable set containing three elements.
     489      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     490      *
     491      * @param <E> the {@code Set}'s element type
     492      * @param e1 the first element
     493      * @param e2 the second element
     494      * @param e3 the third element
     495      * @return a {@code Set} containing the specified elements
     496      * @throws IllegalArgumentException if there are any duplicate elements
     497      * @throws NullPointerException if an element is {@code null}
     498      *
     499      * @since 9
     500      */
     501     static <E> Set<E> of(E e1, E e2, E e3) {
     502         return new ImmutableCollections.SetN<>(e1, e2, e3);
     503     }
     504
     505     /**
     506      * Returns an unmodifiable set containing four elements.
     507      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     508      *
     509      * @param <E> the {@code Set}'s element type
     510      * @param e1 the first element
     511      * @param e2 the second element
     512      * @param e3 the third element
     513      * @param e4 the fourth element
     514      * @return a {@code Set} containing the specified elements
     515      * @throws IllegalArgumentException if there are any duplicate elements
     516      * @throws NullPointerException if an element is {@code null}
     517      *
     518      * @since 9
     519      */
     520     static <E> Set<E> of(E e1, E e2, E e3, E e4) {
     521         return new ImmutableCollections.SetN<>(e1, e2, e3, e4);
     522     }
     523
     524     /**
     525      * Returns an unmodifiable set containing five elements.
     526      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     527      *
     528      * @param <E> the {@code Set}'s element type
     529      * @param e1 the first element
     530      * @param e2 the second element
     531      * @param e3 the third element
     532      * @param e4 the fourth element
     533      * @param e5 the fifth element
     534      * @return a {@code Set} containing the specified elements
     535      * @throws IllegalArgumentException if there are any duplicate elements
     536      * @throws NullPointerException if an element is {@code null}
     537      *
     538      * @since 9
     539      */
     540     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) {
     541         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5);
     542     }
     543
     544     /**
     545      * Returns an unmodifiable set containing six elements.
     546      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     547      *
     548      * @param <E> the {@code Set}'s element type
     549      * @param e1 the first element
     550      * @param e2 the second element
     551      * @param e3 the third element
     552      * @param e4 the fourth element
     553      * @param e5 the fifth element
     554      * @param e6 the sixth element
     555      * @return a {@code Set} containing the specified elements
     556      * @throws IllegalArgumentException if there are any duplicate elements
     557      * @throws NullPointerException if an element is {@code null}
     558      *
     559      * @since 9
     560      */
     561     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
     562         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     563                                                e6);
     564     }
     565
     566     /**
     567      * Returns an unmodifiable set containing seven elements.
     568      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     569      *
     570      * @param <E> the {@code Set}'s element type
     571      * @param e1 the first element
     572      * @param e2 the second element
     573      * @param e3 the third element
     574      * @param e4 the fourth element
     575      * @param e5 the fifth element
     576      * @param e6 the sixth element
     577      * @param e7 the seventh element
     578      * @return a {@code Set} containing the specified elements
     579      * @throws IllegalArgumentException if there are any duplicate elements
     580      * @throws NullPointerException if an element is {@code null}
     581      *
     582      * @since 9
     583      */
     584     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
     585         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     586                                                e6, e7);
     587     }
     588
     589     /**
     590      * Returns an unmodifiable set containing eight elements.
     591      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     592      *
     593      * @param <E> the {@code Set}'s element type
     594      * @param e1 the first element
     595      * @param e2 the second element
     596      * @param e3 the third element
     597      * @param e4 the fourth element
     598      * @param e5 the fifth element
     599      * @param e6 the sixth element
     600      * @param e7 the seventh element
     601      * @param e8 the eighth element
     602      * @return a {@code Set} containing the specified elements
     603      * @throws IllegalArgumentException if there are any duplicate elements
     604      * @throws NullPointerException if an element is {@code null}
     605      *
     606      * @since 9
     607      */
     608     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
     609         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     610                                                e6, e7, e8);
     611     }
     612
     613     /**
     614      * Returns an unmodifiable set containing nine elements.
     615      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     616      *
     617      * @param <E> the {@code Set}'s element type
     618      * @param e1 the first element
     619      * @param e2 the second element
     620      * @param e3 the third element
     621      * @param e4 the fourth element
     622      * @param e5 the fifth element
     623      * @param e6 the sixth element
     624      * @param e7 the seventh element
     625      * @param e8 the eighth element
     626      * @param e9 the ninth element
     627      * @return a {@code Set} containing the specified elements
     628      * @throws IllegalArgumentException if there are any duplicate elements
     629      * @throws NullPointerException if an element is {@code null}
     630      *
     631      * @since 9
     632      */
     633     static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
     634         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     635                                                e6, e7, e8, e9);
     636     }
     637
     638     /**
     639      * Returns an unmodifiable set containing ten elements.
     640      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     641      *
     642      * @param <E> the {@code Set}'s element type
     643      * @param e1 the first element
     644      * @param e2 the second element
     645      * @param e3 the third element
     646      * @param e4 the fourth element
     647      * @param e5 the fifth element
     648      * @param e6 the sixth element
     649      * @param e7 the seventh element
     650      * @param e8 the eighth element
     651      * @param e9 the ninth element
     652      * @param e10 the tenth element
     653      * @return a {@code Set} containing the specified elements
     654      * @throws IllegalArgumentException if there are any duplicate elements
     655      * @throws NullPointerException if an element is {@code null}
     656      *
     657      * @since 9
     658      */
     659     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) {
     660         return new ImmutableCollections.SetN<>(e1, e2, e3, e4, e5,
     661                                                e6, e7, e8, e9, e10);
     662     }
     663
     664     /**
     665      * Returns an unmodifiable set containing an arbitrary number of elements.
     666      * See <a href="#unmodifiable">Unmodifiable Sets</a> for details.
     667      *
     668      * @apiNote
     669      * This method also accepts a single array as an argument. The element type of
     670      * the resulting set will be the component type of the array, and the size of
     671      * the set will be equal to the length of the array. To create a set with
     672      * a single element that is an array, do the following:
     673      *
     674      * <pre>{@code
     675      *     String[] array = ... ;
     676      *     Set<String[]> list = Set.<String[]>of(array);
     677      * }</pre>
     678      *
     679      * This will cause the {@link Set#of(Object) Set.of(E)} method
     680      * to be invoked instead.
     681      *
     682      * @param <E> the {@code Set}'s element type
     683      * @param elements the elements to be contained in the set
     684      * @return a {@code Set} containing the specified elements
     685      * @throws IllegalArgumentException if there are any duplicate elements
     686      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
     687      *
     688      * @since 9
     689      */
     690     @SafeVarargs
     691     @SuppressWarnings("varargs")
     692     static <E> Set<E> of(E... elements) {
     693         switch (elements.length) { // implicit null check of elements
     694             case 0:
     695                 return ImmutableCollections.Set0.instance();
     696             case 1:
     697                 return new ImmutableCollections.Set1<>(elements[0]);
     698             case 2:
     699                 return new ImmutableCollections.Set2<>(elements[0], elements[1]);
     700             default:
     701                 return new ImmutableCollections.SetN<>(elements);
     702         }
     703     }
     704
     705     /**
     706      * Returns an <a href="#unmodifiable">unmodifiable Set</a> containing the elements
     707      * of the given Collection. The given Collection must not be null, and it must not
     708      * contain any null elements. If the given Collection contains duplicate elements,
     709      * an arbitrary element of the duplicates is preserved. If the given Collection is
     710      * subsequently modified, the returned Set will not reflect such modifications.
     711      *
     712      * @implNote
     713      * If the given Collection is an <a href="#unmodifiable">unmodifiable Set</a>,
     714      * calling copyOf will generally not create a copy.
     715      *
     716      * @param <E> the {@code Set}'s element type
     717      * @param coll a {@code Collection} from which elements are drawn, must be non-null
     718      * @return a {@code Set} containing the elements of the given {@code Collection}
     719      * @throws NullPointerException if coll is null, or if it contains any nulls
     720      * @since 10
     721      */
     722     @SuppressWarnings("unchecked")
     723     static <E> Set<E> copyOf(Collection<? extends E> coll) {
     724         if (coll instanceof ImmutableCollections.AbstractImmutableSet) {
     725             return (Set<E>)coll;
     726         } else {
     727             return (Set<E>)Set.of(new HashSet<>(coll).toArray());
     728         }
     729     }
     730 }

< prev index next >