< prev index next >

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

Print this page




 770      */
 771     @Override
 772     default Spliterator<E> spliterator() {
 773         if (this instanceof RandomAccess) {
 774             return new AbstractList.RandomAccessSpliterator<>(this);
 775         } else {
 776             return Spliterators.spliterator(this, Spliterator.ORDERED);
 777         }
 778     }
 779 
 780     /**
 781      * Returns an unmodifiable list containing zero elements.
 782      *
 783      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 784      *
 785      * @param <E> the {@code List}'s element type
 786      * @return an empty {@code List}
 787      *
 788      * @since 9
 789      */
 790     @SuppressWarnings("unchecked")
 791     static <E> List<E> of() {
 792         return (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
 793     }
 794 
 795     /**
 796      * Returns an unmodifiable list containing one element.
 797      *
 798      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 799      *
 800      * @param <E> the {@code List}'s element type
 801      * @param e1 the single element
 802      * @return a {@code List} containing the specified element
 803      * @throws NullPointerException if the element is {@code null}
 804      *
 805      * @since 9
 806      */
 807     static <E> List<E> of(E e1) {
 808         return new ImmutableCollections.List12<>(e1);
 809     }
 810 
 811     /**
 812      * Returns an unmodifiable list containing two elements.


1015      * <pre>{@code
1016      *     String[] array = ... ;
1017      *     List<String[]> list = List.<String[]>of(array);
1018      * }</pre>
1019      *
1020      * This will cause the {@link List#of(Object) List.of(E)} method
1021      * to be invoked instead.
1022      *
1023      * @param <E> the {@code List}'s element type
1024      * @param elements the elements to be contained in the list
1025      * @return a {@code List} containing the specified elements
1026      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1027      *
1028      * @since 9
1029      */
1030     @SafeVarargs
1031     @SuppressWarnings("varargs")
1032     static <E> List<E> of(E... elements) {
1033         switch (elements.length) { // implicit null check of elements
1034             case 0:
1035                 @SuppressWarnings("unchecked")
1036                 var list = (List<E>) ImmutableCollections.ListN.EMPTY_LIST;
1037                 return list;
1038             case 1:
1039                 return new ImmutableCollections.List12<>(elements[0]);
1040             case 2:
1041                 return new ImmutableCollections.List12<>(elements[0], elements[1]);
1042             default:
1043                 return new ImmutableCollections.ListN<>(elements);
1044         }
1045     }
1046 
1047     /**
1048      * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of
1049      * the given Collection, in its iteration order. The given Collection must not be null,
1050      * and it must not contain any null elements. If the given Collection is subsequently
1051      * modified, the returned List will not reflect such modifications.
1052      *
1053      * @implNote
1054      * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,
1055      * calling copyOf will generally not create a copy.
1056      *
1057      * @param <E> the {@code List}'s element type


 770      */
 771     @Override
 772     default Spliterator<E> spliterator() {
 773         if (this instanceof RandomAccess) {
 774             return new AbstractList.RandomAccessSpliterator<>(this);
 775         } else {
 776             return Spliterators.spliterator(this, Spliterator.ORDERED);
 777         }
 778     }
 779 
 780     /**
 781      * Returns an unmodifiable list containing zero elements.
 782      *
 783      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 784      *
 785      * @param <E> the {@code List}'s element type
 786      * @return an empty {@code List}
 787      *
 788      * @since 9
 789      */

 790     static <E> List<E> of() {
 791         return ImmutableCollections.emptyList();
 792     }
 793 
 794     /**
 795      * Returns an unmodifiable list containing one element.
 796      *
 797      * See <a href="#unmodifiable">Unmodifiable Lists</a> for details.
 798      *
 799      * @param <E> the {@code List}'s element type
 800      * @param e1 the single element
 801      * @return a {@code List} containing the specified element
 802      * @throws NullPointerException if the element is {@code null}
 803      *
 804      * @since 9
 805      */
 806     static <E> List<E> of(E e1) {
 807         return new ImmutableCollections.List12<>(e1);
 808     }
 809 
 810     /**
 811      * Returns an unmodifiable list containing two elements.


1014      * <pre>{@code
1015      *     String[] array = ... ;
1016      *     List<String[]> list = List.<String[]>of(array);
1017      * }</pre>
1018      *
1019      * This will cause the {@link List#of(Object) List.of(E)} method
1020      * to be invoked instead.
1021      *
1022      * @param <E> the {@code List}'s element type
1023      * @param elements the elements to be contained in the list
1024      * @return a {@code List} containing the specified elements
1025      * @throws NullPointerException if an element is {@code null} or if the array is {@code null}
1026      *
1027      * @since 9
1028      */
1029     @SafeVarargs
1030     @SuppressWarnings("varargs")
1031     static <E> List<E> of(E... elements) {
1032         switch (elements.length) { // implicit null check of elements
1033             case 0:
1034                 return ImmutableCollections.emptyList();


1035             case 1:
1036                 return new ImmutableCollections.List12<>(elements[0]);
1037             case 2:
1038                 return new ImmutableCollections.List12<>(elements[0], elements[1]);
1039             default:
1040                 return new ImmutableCollections.ListN<>(elements);
1041         }
1042     }
1043 
1044     /**
1045      * Returns an <a href="#unmodifiable">unmodifiable List</a> containing the elements of
1046      * the given Collection, in its iteration order. The given Collection must not be null,
1047      * and it must not contain any null elements. If the given Collection is subsequently
1048      * modified, the returned List will not reflect such modifications.
1049      *
1050      * @implNote
1051      * If the given Collection is an <a href="#unmodifiable">unmodifiable List</a>,
1052      * calling copyOf will generally not create a copy.
1053      *
1054      * @param <E> the {@code List}'s element type
< prev index next >