Returns an immutable set containing each of elements, minus duplicates, in the order each appears first in the source collection. Performance note: This method will sometimes recognize that the actual copy operation is unnecessary; for example, copyOf(copyOf(anArrayList))
(Collection<? extends E> elements)
| 230 | * @since 7.0 (source-compatible since 2.0) |
| 231 | */ |
| 232 | public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) { |
| 233 | /* |
| 234 | * TODO(lowasser): consider checking for ImmutableAsList here |
| 235 | * TODO(lowasser): consider checking for Multiset here |
| 236 | */ |
| 237 | if (elements instanceof ImmutableSet && !(elements instanceof ImmutableSortedSet)) { |
| 238 | @SuppressWarnings("unchecked") // all supported methods are covariant |
| 239 | ImmutableSet<E> set = (ImmutableSet<E>) elements; |
| 240 | if (!set.isPartialView()) { |
| 241 | return set; |
| 242 | } |
| 243 | } else if (elements instanceof EnumSet) { |
| 244 | return copyOfEnumSet((EnumSet) elements); |
| 245 | } |
| 246 | Object[] array = elements.toArray(); |
| 247 | return construct(array.length, array); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order |
no test coverage detected