2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.android.collect;
19 import java.util.Collections;
20 import java.util.EnumSet;
21 import java.util.HashSet;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
26 * Provides static methods for creating mutable {@code Set} instances easily and
27 * other static methods for working with Sets.
33 * Creates an empty {@code HashSet} instance.
35 * <p><b>Note:</b> if {@code E} is an {@link Enum} type, use {@link
36 * EnumSet#noneOf} instead.
38 * <p><b>Note:</b> if you only need an <i>immutable</i> empty Set,
39 * use {@link Collections#emptySet} instead.
41 * @return a newly-created, initially-empty {@code HashSet}
43 public static <K> HashSet<K> newHashSet() {
44 return new HashSet<K>();
48 * Creates a {@code HashSet} instance containing the given elements.
50 * <p><b>Note:</b> due to a bug in javac 1.5.0_06, we cannot support the
53 * <p>{@code Set<Base> set = Sets.newHashSet(sub1, sub2);}
55 * <p>where {@code sub1} and {@code sub2} are references to subtypes of {@code
56 * Base}, not of {@code Base} itself. To get around this, you must use:
58 * <p>{@code Set<Base> set = Sets.<Base>newHashSet(sub1, sub2);}
60 * @param elements the elements that the set should contain
61 * @return a newly-created {@code HashSet} containing those elements (minus
64 public static <E> HashSet<E> newHashSet(E... elements) {
65 int capacity = elements.length * 4 / 3 + 1;
66 HashSet<E> set = new HashSet<E>(capacity);
67 Collections.addAll(set, elements);
72 * Creates an empty {@code SortedSet} instance.
74 * @return a newly-created, initially-empty {@code SortedSet}.
76 public static <E> SortedSet<E> newSortedSet() {
77 return new TreeSet<E>();
81 * Creates a {@code SortedSet} instance containing the given elements.
83 * @param elements the elements that the set should contain
84 * @return a newly-created {@code SortedSet} containing those elements (minus
87 public static <E> SortedSet<E> newSortedSet(E... elements) {
88 SortedSet<E> set = new TreeSet<E>();
89 Collections.addAll(set, elements);