--- old/src/java.base/share/classes/java/util/Set.java 2017-09-20 16:53:10.000000000 -0700
+++ new/src/java.base/share/classes/java/util/Set.java 2017-09-20 16:53:10.000000000 -0700
@@ -63,15 +63,16 @@
* Such exceptions are marked as "optional" in the specification for this
* interface.
*
- *
- * The {@link Set#of(Object...) Set.of()} static factory methods
- * provide a convenient way to create immutable sets. The {@code Set}
+ *
+ * The {@link Set#of(Object...) Set.of} and
+ * {@link Set#copyOf Set.copyOf} static factory methods
+ * provide a convenient way to create unmodifiable sets. The {@code Set}
* instances created by these methods have the following characteristics:
*
*
- * - They are structurally immutable. Elements cannot be added or
- * removed. Calling any mutator method will always cause
- * {@code UnsupportedOperationException} to be thrown.
+ *
- They are unmodifiable. Elements cannot
+ * be added or removed. Calling any mutator method on the Set
+ * will always cause {@code UnsupportedOperationException} to be thrown.
* However, if the contained elements are themselves mutable, this may cause the
* Set to behave inconsistently or its contents to appear to change.
*
- They disallow {@code null} elements. Attempts to create them with
@@ -439,8 +440,8 @@
}
/**
- * Returns an immutable set containing zero elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing zero elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @return an empty {@code Set}
@@ -452,8 +453,8 @@
}
/**
- * Returns an immutable set containing one element.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing one element.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the single element
@@ -467,8 +468,8 @@
}
/**
- * Returns an immutable set containing two elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing two elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -484,8 +485,8 @@
}
/**
- * Returns an immutable set containing three elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing three elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -502,8 +503,8 @@
}
/**
- * Returns an immutable set containing four elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing four elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -521,8 +522,8 @@
}
/**
- * Returns an immutable set containing five elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing five elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -541,8 +542,8 @@
}
/**
- * Returns an immutable set containing six elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing six elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -563,8 +564,8 @@
}
/**
- * Returns an immutable set containing seven elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing seven elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -586,8 +587,8 @@
}
/**
- * Returns an immutable set containing eight elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing eight elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -610,8 +611,8 @@
}
/**
- * Returns an immutable set containing nine elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing nine elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -635,8 +636,8 @@
}
/**
- * Returns an immutable set containing ten elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing ten elements.
+ * See Unmodifiable Sets for details.
*
* @param the {@code Set}'s element type
* @param e1 the first element
@@ -661,8 +662,8 @@
}
/**
- * Returns an immutable set containing an arbitrary number of elements.
- * See Immutable Set Static Factory Methods for details.
+ * Returns an unmodifiable set containing an arbitrary number of elements.
+ * See Unmodifiable Sets for details.
*
* @apiNote
* This method also accepts a single array as an argument. The element type of
@@ -700,4 +701,26 @@
return new ImmutableCollections.SetN<>(elements);
}
}
+
+ /**
+ * Returns an unmodifiable Set containing the elements of the given Collection.
+ * Duplicate elements are permitted. If the Collection has a defined iteration order,
+ * the first element of any duplicates is preserved, and the rest are discarded.
+ * Otherwise, an arbitrary element is preserved.
+ * See Unmodifiable Sets for
+ * information about the characteristics of the returned Set.
+ *
+ * @param the {@code Set}'s element type
+ * @param coll the collection from which elements are drawn
+ * @return the new Set
+ * @since 10
+ */
+ @SuppressWarnings("unchecked")
+ static Set copyOf(Collection extends E> coll) {
+ if (coll instanceof ImmutableCollections.AbstractImmutableSet) {
+ return (Set)coll;
+ } else {
+ return (Set)Set.of(coll.stream().distinct().toArray());
+ }
+ }
}