blob: dd3cab15d0b2769282ae755cb780d245056362a0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.google.android.collect;
18
Jeff Sharkey43f42632013-06-18 09:27:23 -070019import android.util.ArraySet;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import java.util.Collections;
22import java.util.EnumSet;
23import java.util.HashSet;
24import java.util.SortedSet;
25import java.util.TreeSet;
26
27/**
28 * Provides static methods for creating mutable {@code Set} instances easily and
29 * other static methods for working with Sets.
30 *
31 */
32public class Sets {
33
34 /**
35 * Creates an empty {@code HashSet} instance.
36 *
37 * <p><b>Note:</b> if {@code E} is an {@link Enum} type, use {@link
38 * EnumSet#noneOf} instead.
39 *
40 * <p><b>Note:</b> if you only need an <i>immutable</i> empty Set,
41 * use {@link Collections#emptySet} instead.
42 *
43 * @return a newly-created, initially-empty {@code HashSet}
44 */
45 public static <K> HashSet<K> newHashSet() {
46 return new HashSet<K>();
47 }
48
Evan Millar049070d2009-12-07 14:01:33 -080049 /**
50 * Creates a {@code HashSet} instance containing the given elements.
51 *
52 * <p><b>Note:</b> due to a bug in javac 1.5.0_06, we cannot support the
53 * following:
54 *
55 * <p>{@code Set<Base> set = Sets.newHashSet(sub1, sub2);}
56 *
57 * <p>where {@code sub1} and {@code sub2} are references to subtypes of {@code
58 * Base}, not of {@code Base} itself. To get around this, you must use:
59 *
60 * <p>{@code Set<Base> set = Sets.<Base>newHashSet(sub1, sub2);}
61 *
62 * @param elements the elements that the set should contain
63 * @return a newly-created {@code HashSet} containing those elements (minus
64 * duplicates)
65 */
66 public static <E> HashSet<E> newHashSet(E... elements) {
67 int capacity = elements.length * 4 / 3 + 1;
68 HashSet<E> set = new HashSet<E>(capacity);
69 Collections.addAll(set, elements);
70 return set;
71 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Evan Millar049070d2009-12-07 14:01:33 -080073 /**
74 * Creates an empty {@code SortedSet} instance.
75 *
76 * @return a newly-created, initially-empty {@code SortedSet}.
77 */
78 public static <E> SortedSet<E> newSortedSet() {
79 return new TreeSet<E>();
80 }
81
82 /**
83 * Creates a {@code SortedSet} instance containing the given elements.
84 *
85 * @param elements the elements that the set should contain
86 * @return a newly-created {@code SortedSet} containing those elements (minus
87 * duplicates)
88 */
89 public static <E> SortedSet<E> newSortedSet(E... elements) {
90 SortedSet<E> set = new TreeSet<E>();
91 Collections.addAll(set, elements);
92 return set;
93 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Jeff Sharkey43f42632013-06-18 09:27:23 -070095 /**
96 * Creates a {@code ArraySet} instance.
97 */
98 public static <E> ArraySet<E> newArraySet() {
99 return new ArraySet<E>();
100 }
101
102 /**
103 * Creates a {@code ArraySet} instance containing the given elements.
104 */
105 public static <E> ArraySet<E> newArraySet(E... elements) {
106 int capacity = elements.length * 4 / 3 + 1;
107 ArraySet<E> set = new ArraySet<E>(capacity);
108 Collections.addAll(set, elements);
109 return set;
110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111}