< prev index next >

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

Print this page
rev 14011 : Change @since 1.9 to @since 9.
rev 14012 : Add disclaimer about mutable elements.
rev 14013 : Fix typos.

@@ -61,10 +61,27 @@
  * the insertion of an ineligible element into the set 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 Set Static Factory Methods</a></h2>
+ * <p>The {@link Set#of(Object...) Set.of()} static factory methods
+ * provide a convenient way to create immutable sets. The {@code Set}
+ * 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 are truly immutable only if the contained elements are themselves
+ * immutable. If an element is mutated, the behavior of the set is unspecified.
+ * <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.
+ * <li>They reject duplicate elements at creation time. Duplicate elements
+ * passed to a static factory method result in {@code IllegalArgumentException}.
+ * </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 maintained by this set

@@ -408,6 +425,344 @@
      */
     @Override
     default Spliterator<E> spliterator() {
         return Spliterators.spliterator(this, Spliterator.DISTINCT);
     }
+
+    /**
+     * Creates an immutable set containing zero elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @return the newly created set
+     *
+     * @since 9
+     */
+    static <E> Set<E> of() {
+        return Collections.emptySet();
+    }
+
+    /**
+     * Creates an immutable set containing one element.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the single set element
+     * @return the newly created set
+     * @throws NullPointerException if the element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1) {
+        return Collections.singleton(Objects.requireNonNull(e1));
+    }
+
+    /**
+     * Creates an immutable set containing two elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if the elements are duplicates
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2)));
+        if (set.size() != 2) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing three elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2), 
+                                                 Objects.requireNonNull(e3)));
+        if (set.size() != 3) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing four elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2), 
+                                                 Objects.requireNonNull(e3),
+                                                 Objects.requireNonNull(e4)));
+        if (set.size() != 4) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing five elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2), 
+                                                 Objects.requireNonNull(e3),
+                                                 Objects.requireNonNull(e4),
+                                                 Objects.requireNonNull(e5)));
+        if (set.size() != 5) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing six elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @param e6 the sixth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2), 
+                                                 Objects.requireNonNull(e3),
+                                                 Objects.requireNonNull(e4),
+                                                 Objects.requireNonNull(e5),
+                                                 Objects.requireNonNull(e6)));
+        if (set.size() != 6) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing seven elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @param e6 the sixth set element
+     * @param e7 the seventh set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {
+        Set<E> set = new HashSet<>(Arrays.asList(Objects.requireNonNull(e1),
+                                                 Objects.requireNonNull(e2), 
+                                                 Objects.requireNonNull(e3),
+                                                 Objects.requireNonNull(e4),
+                                                 Objects.requireNonNull(e5),
+                                                 Objects.requireNonNull(e6),
+                                                 Objects.requireNonNull(e7)));
+        if (set.size() != 7) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing eight elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @param e6 the sixth set element
+     * @param e7 the seventh set element
+     * @param e8 the eighth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {
+        Set<E> set = new HashSet<>(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)));
+        if (set.size() != 8) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing nine elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @param e6 the sixth set element
+     * @param e7 the seventh set element
+     * @param e8 the eighth set element
+     * @param e9 the ninth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {
+        Set<E> set = new HashSet<>(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)));
+        if (set.size() != 9) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing ten elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @param <E> the set's element type
+     * @param e1 the first set element
+     * @param e2 the second set element
+     * @param e3 the third set element
+     * @param e4 the fourth set element
+     * @param e5 the fifth set element
+     * @param e6 the sixth set element
+     * @param e7 the seventh set element
+     * @param e8 the eighth set element
+     * @param e9 the ninth set element
+     * @param e10 the tenth set element
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null
+     *
+     * @since 9
+     */
+    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) {
+        Set<E> set = new HashSet<>(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)));
+        if (set.size() != 10) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
+
+    /**
+     * Creates an immutable set containing an arbitrary number of elements.
+     * See <a href="#immutable">Immutable Set Static Factory Methods</a> for details.
+     *
+     * @apiNote
+     * 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:
+     *
+     * <pre>{@code
+     *     String[] array = ... ;
+     *     Set<String[]> list = Set.<String[]>of(array);
+     * }</pre>
+     *
+     * This will cause the {@link Set#of(Object) Set.of(E)} method
+     * to be invoked instead.
+     *
+     * @param <E> the set's element type
+     * @param es the elements to be contained in the set
+     * @return the newly created set
+     * @throws IllegalArgumentException if there are any duplicate elements
+     * @throws NullPointerException if an element is null or if the array is null
+     *
+     * @since 9
+     */
+    @SafeVarargs
+    @SuppressWarnings("varargs")
+    static <E> Set<E> of(E... es) {
+        for (E e : es) { // throws NPE if es is null
+            Objects.requireNonNull(e);
+        }
+        // NOTE: this can allow a null element to slip through
+        Set<E> set = new HashSet<>(Arrays.asList(es));
+        if (set.size() != es.length) {
+            throw new IllegalArgumentException("duplicate elements");
+        }
+        return Collections.unmodifiableSet(set);
+    }
 }
< prev index next >