< prev index next >

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

Print this page
rev 48077 : 8193128: Reduce number of implementation classes returned by List/Set/Map.of()
Reviewed-by: smarks


 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 


 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


 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.emptySet();
 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.Set12<>(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.Set12<>(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 


 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.emptySet();
 696             case 1:
 697                 return new ImmutableCollections.Set12<>(elements[0]);
 698             case 2:
 699                 return new ImmutableCollections.Set12<>(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
< prev index next >