< prev index next >

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

Print this page
rev 13824 : Remove factories from concrete collections. Increase counts to 10.

@@ -85,10 +85,23 @@
  * the insertion of an ineligible element into the list may throw an
  * exception or it may succeed, at the option of the implementation.
  * Such exceptions are marked as "optional" in the specification for this
  * interface.
  *
+ * <h2><a name="immutable">Immutable List Factory Methods</a></h2>
+ * <p>The {@link List#of(Object...) List.of()} factory methods
+ * provide a convenient way to create immutable lists. The {@code List}
+ * instances created by these methods have the following characteristics:
+ *
+ * <ul>
+ * <li>They cannot be modified. Attempts to modify them result in
+ * an {@code UnsupportedOperationException}.
+ * <li>They disallow null elements. Attempts to create them with
+ * null elements result in {@code NullPointerException}.
+ * <li>They are serializable if all elements are serializable.
+ * </ul>
+ *
  * <p>This interface is a member of the
  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
  * Java Collections Framework</a>.
  *
  * @param <E> the type of elements in this list

@@ -729,6 +742,315 @@
      */
     @Override
     default Spliterator<E> spliterator() {
         return Spliterators.spliterator(this, Spliterator.ORDERED);
     }
+
+    /**
+     * Creates an immutable list containing zero elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @return the newly created list
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of() {
+        return Collections.emptyList();
+    }
+
+    /**
+     * Creates an immutable list containing one element.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the single list element
+     * @return the newly created list
+     * @throws NullPointerException if the element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1) {
+        return Collections.singletonList(Objects.requireNonNull(e1));
+    }
+
+    /**
+     * Creates an immutable list containing two elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2)));
+    }
+
+    /**
+     * Creates an immutable list containing three elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3)));
+    }
+
+    /**
+     * Creates an immutable list containing four elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4)));
+    }
+
+    /**
+     * Creates an immutable list containing five elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5)));
+    }
+
+    /**
+     * Creates an immutable list containing six elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @param e6 the sixth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5),
+                          Objects.requireNonNull(e6)));
+    }
+
+    /**
+     * Creates an immutable list containing seven elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @param e6 the sixth list element
+     * @param e7 the seventh list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5),
+                          Objects.requireNonNull(e6),
+                          Objects.requireNonNull(e7)));
+    }
+
+    /**
+     * Creates an immutable list containing eight elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @param e6 the sixth list element
+     * @param e7 the seventh list element
+     * @param e8 the eighth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5),
+                          Objects.requireNonNull(e6),
+                          Objects.requireNonNull(e7),
+                          Objects.requireNonNull(e8)));
+    }
+
+    /**
+     * Creates an immutable list containing nine elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @param e6 the sixth list element
+     * @param e7 the seventh list element
+     * @param e8 the eighth list element
+     * @param e9 the ninth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5),
+                          Objects.requireNonNull(e6),
+                          Objects.requireNonNull(e7),
+                          Objects.requireNonNull(e8),
+                          Objects.requireNonNull(e9)));
+    }
+
+    /**
+     * Creates an immutable list containing ten elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @param <E> the list's element type
+     * @param e1 the first list element
+     * @param e2 the second list element
+     * @param e3 the third list element
+     * @param e4 the fourth list element
+     * @param e5 the fifth list element
+     * @param e6 the sixth list element
+     * @param e7 the seventh list element
+     * @param e8 the eighth list element
+     * @param e9 the ninth list element
+     * @param e10 the tenth list element
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {
+        return Collections.unmodifiableList(
+            Arrays.asList(Objects.requireNonNull(e1),
+                          Objects.requireNonNull(e2),
+                          Objects.requireNonNull(e3),
+                          Objects.requireNonNull(e4),
+                          Objects.requireNonNull(e5),
+                          Objects.requireNonNull(e6),
+                          Objects.requireNonNull(e7),
+                          Objects.requireNonNull(e8),
+                          Objects.requireNonNull(e9),
+                          Objects.requireNonNull(e10)));
+    }
+
+    /**
+     * Creates an immutable list containing an arbitrary number of elements.
+     *
+     * See <a href="#immutable">Immutable List Factory Methods</a> for details.
+     *
+     * @apiNote
+     * This method also accepts a single array as an argument. The element type of
+     * the resulting list will be the component type of the array, and the size of
+     * the list will be equal to the length of the array. To create a list with
+     * a single element that is an array, do the following:
+     *
+     * <pre>{@code
+     *     String[] array = ... ;
+     *     List<String[]> list = List.<String[]>of(array);
+     * }</pre>
+     *
+     * This will cause the {@link List#of(Object) List.of(E)} method
+     * to be invoked instead.
+     *
+     * @param <E> the list's element type
+     * @param es the elements to be contained in the list
+     * @return the newly created list
+     * @throws NullPointerException if an element is null
+     *
+     * @since 1.9
+     */
+    @SafeVarargs
+    @SuppressWarnings("varargs")
+    static <E> List<E> of(E... es) {
+        for (E e : es) {
+            Objects.requireNonNull(e);
+        }
+        // NOTE: this can allow a null element to slip through
+        return Collections.unmodifiableList(Arrays.asList(es));
+    }
 }
< prev index next >