jaroslav@557
|
1 |
/*
|
jaroslav@557
|
2 |
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@557
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@557
|
4 |
*
|
jaroslav@557
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@557
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@557
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@557
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@557
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@557
|
10 |
*
|
jaroslav@557
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@557
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@557
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@557
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@557
|
15 |
* accompanied this code).
|
jaroslav@557
|
16 |
*
|
jaroslav@557
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@557
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@557
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@557
|
20 |
*
|
jaroslav@557
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@557
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@557
|
23 |
* questions.
|
jaroslav@557
|
24 |
*/
|
jaroslav@557
|
25 |
|
jaroslav@557
|
26 |
package java.util;
|
jaroslav@557
|
27 |
|
jaroslav@557
|
28 |
/**
|
jaroslav@557
|
29 |
* This class implements the <tt>Set</tt> interface, backed by a hash table
|
jaroslav@557
|
30 |
* (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the
|
jaroslav@557
|
31 |
* iteration order of the set; in particular, it does not guarantee that the
|
jaroslav@557
|
32 |
* order will remain constant over time. This class permits the <tt>null</tt>
|
jaroslav@557
|
33 |
* element.
|
jaroslav@557
|
34 |
*
|
jaroslav@557
|
35 |
* <p>This class offers constant time performance for the basic operations
|
jaroslav@557
|
36 |
* (<tt>add</tt>, <tt>remove</tt>, <tt>contains</tt> and <tt>size</tt>),
|
jaroslav@557
|
37 |
* assuming the hash function disperses the elements properly among the
|
jaroslav@557
|
38 |
* buckets. Iterating over this set requires time proportional to the sum of
|
jaroslav@557
|
39 |
* the <tt>HashSet</tt> instance's size (the number of elements) plus the
|
jaroslav@557
|
40 |
* "capacity" of the backing <tt>HashMap</tt> instance (the number of
|
jaroslav@557
|
41 |
* buckets). Thus, it's very important not to set the initial capacity too
|
jaroslav@557
|
42 |
* high (or the load factor too low) if iteration performance is important.
|
jaroslav@557
|
43 |
*
|
jaroslav@557
|
44 |
* <p><strong>Note that this implementation is not synchronized.</strong>
|
jaroslav@557
|
45 |
* If multiple threads access a hash set concurrently, and at least one of
|
jaroslav@557
|
46 |
* the threads modifies the set, it <i>must</i> be synchronized externally.
|
jaroslav@557
|
47 |
* This is typically accomplished by synchronizing on some object that
|
jaroslav@557
|
48 |
* naturally encapsulates the set.
|
jaroslav@557
|
49 |
*
|
jaroslav@557
|
50 |
* If no such object exists, the set should be "wrapped" using the
|
jaroslav@557
|
51 |
* {@link Collections#synchronizedSet Collections.synchronizedSet}
|
jaroslav@557
|
52 |
* method. This is best done at creation time, to prevent accidental
|
jaroslav@557
|
53 |
* unsynchronized access to the set:<pre>
|
jaroslav@557
|
54 |
* Set s = Collections.synchronizedSet(new HashSet(...));</pre>
|
jaroslav@557
|
55 |
*
|
jaroslav@557
|
56 |
* <p>The iterators returned by this class's <tt>iterator</tt> method are
|
jaroslav@557
|
57 |
* <i>fail-fast</i>: if the set is modified at any time after the iterator is
|
jaroslav@557
|
58 |
* created, in any way except through the iterator's own <tt>remove</tt>
|
jaroslav@557
|
59 |
* method, the Iterator throws a {@link ConcurrentModificationException}.
|
jaroslav@557
|
60 |
* Thus, in the face of concurrent modification, the iterator fails quickly
|
jaroslav@557
|
61 |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at
|
jaroslav@557
|
62 |
* an undetermined time in the future.
|
jaroslav@557
|
63 |
*
|
jaroslav@557
|
64 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
jaroslav@557
|
65 |
* as it is, generally speaking, impossible to make any hard guarantees in the
|
jaroslav@557
|
66 |
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
jaroslav@557
|
67 |
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
|
jaroslav@557
|
68 |
* Therefore, it would be wrong to write a program that depended on this
|
jaroslav@557
|
69 |
* exception for its correctness: <i>the fail-fast behavior of iterators
|
jaroslav@557
|
70 |
* should be used only to detect bugs.</i>
|
jaroslav@557
|
71 |
*
|
jaroslav@557
|
72 |
* <p>This class is a member of the
|
jaroslav@557
|
73 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
jaroslav@557
|
74 |
* Java Collections Framework</a>.
|
jaroslav@557
|
75 |
*
|
jaroslav@557
|
76 |
* @param <E> the type of elements maintained by this set
|
jaroslav@557
|
77 |
*
|
jaroslav@557
|
78 |
* @author Josh Bloch
|
jaroslav@557
|
79 |
* @author Neal Gafter
|
jaroslav@557
|
80 |
* @see Collection
|
jaroslav@557
|
81 |
* @see Set
|
jaroslav@557
|
82 |
* @see TreeSet
|
jaroslav@557
|
83 |
* @see HashMap
|
jaroslav@557
|
84 |
* @since 1.2
|
jaroslav@557
|
85 |
*/
|
jaroslav@557
|
86 |
|
jaroslav@557
|
87 |
public class HashSet<E>
|
jaroslav@557
|
88 |
extends AbstractSet<E>
|
jaroslav@557
|
89 |
implements Set<E>, Cloneable, java.io.Serializable
|
jaroslav@557
|
90 |
{
|
jaroslav@557
|
91 |
static final long serialVersionUID = -5024744406713321676L;
|
jaroslav@557
|
92 |
|
jaroslav@557
|
93 |
private transient HashMap<E,Object> map;
|
jaroslav@557
|
94 |
|
jaroslav@557
|
95 |
// Dummy value to associate with an Object in the backing Map
|
jaroslav@557
|
96 |
private static final Object PRESENT = new Object();
|
jaroslav@557
|
97 |
|
jaroslav@557
|
98 |
/**
|
jaroslav@557
|
99 |
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
jaroslav@557
|
100 |
* default initial capacity (16) and load factor (0.75).
|
jaroslav@557
|
101 |
*/
|
jaroslav@557
|
102 |
public HashSet() {
|
jaroslav@557
|
103 |
map = new HashMap<>();
|
jaroslav@557
|
104 |
}
|
jaroslav@557
|
105 |
|
jaroslav@557
|
106 |
/**
|
jaroslav@557
|
107 |
* Constructs a new set containing the elements in the specified
|
jaroslav@557
|
108 |
* collection. The <tt>HashMap</tt> is created with default load factor
|
jaroslav@557
|
109 |
* (0.75) and an initial capacity sufficient to contain the elements in
|
jaroslav@557
|
110 |
* the specified collection.
|
jaroslav@557
|
111 |
*
|
jaroslav@557
|
112 |
* @param c the collection whose elements are to be placed into this set
|
jaroslav@557
|
113 |
* @throws NullPointerException if the specified collection is null
|
jaroslav@557
|
114 |
*/
|
jaroslav@557
|
115 |
public HashSet(Collection<? extends E> c) {
|
jaroslav@557
|
116 |
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
|
jaroslav@557
|
117 |
addAll(c);
|
jaroslav@557
|
118 |
}
|
jaroslav@557
|
119 |
|
jaroslav@557
|
120 |
/**
|
jaroslav@557
|
121 |
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
jaroslav@557
|
122 |
* the specified initial capacity and the specified load factor.
|
jaroslav@557
|
123 |
*
|
jaroslav@557
|
124 |
* @param initialCapacity the initial capacity of the hash map
|
jaroslav@557
|
125 |
* @param loadFactor the load factor of the hash map
|
jaroslav@557
|
126 |
* @throws IllegalArgumentException if the initial capacity is less
|
jaroslav@557
|
127 |
* than zero, or if the load factor is nonpositive
|
jaroslav@557
|
128 |
*/
|
jaroslav@557
|
129 |
public HashSet(int initialCapacity, float loadFactor) {
|
jaroslav@557
|
130 |
map = new HashMap<>(initialCapacity, loadFactor);
|
jaroslav@557
|
131 |
}
|
jaroslav@557
|
132 |
|
jaroslav@557
|
133 |
/**
|
jaroslav@557
|
134 |
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
jaroslav@557
|
135 |
* the specified initial capacity and default load factor (0.75).
|
jaroslav@557
|
136 |
*
|
jaroslav@557
|
137 |
* @param initialCapacity the initial capacity of the hash table
|
jaroslav@557
|
138 |
* @throws IllegalArgumentException if the initial capacity is less
|
jaroslav@557
|
139 |
* than zero
|
jaroslav@557
|
140 |
*/
|
jaroslav@557
|
141 |
public HashSet(int initialCapacity) {
|
jaroslav@557
|
142 |
map = new HashMap<>(initialCapacity);
|
jaroslav@557
|
143 |
}
|
jaroslav@557
|
144 |
|
jaroslav@557
|
145 |
/**
|
jaroslav@557
|
146 |
* Constructs a new, empty linked hash set. (This package private
|
jaroslav@557
|
147 |
* constructor is only used by LinkedHashSet.) The backing
|
jaroslav@557
|
148 |
* HashMap instance is a LinkedHashMap with the specified initial
|
jaroslav@557
|
149 |
* capacity and the specified load factor.
|
jaroslav@557
|
150 |
*
|
jaroslav@557
|
151 |
* @param initialCapacity the initial capacity of the hash map
|
jaroslav@557
|
152 |
* @param loadFactor the load factor of the hash map
|
jaroslav@557
|
153 |
* @param dummy ignored (distinguishes this
|
jaroslav@557
|
154 |
* constructor from other int, float constructor.)
|
jaroslav@557
|
155 |
* @throws IllegalArgumentException if the initial capacity is less
|
jaroslav@557
|
156 |
* than zero, or if the load factor is nonpositive
|
jaroslav@557
|
157 |
*/
|
jaroslav@557
|
158 |
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
|
jaroslav@557
|
159 |
map = new LinkedHashMap<>(initialCapacity, loadFactor);
|
jaroslav@557
|
160 |
}
|
jaroslav@557
|
161 |
|
jaroslav@557
|
162 |
/**
|
jaroslav@557
|
163 |
* Returns an iterator over the elements in this set. The elements
|
jaroslav@557
|
164 |
* are returned in no particular order.
|
jaroslav@557
|
165 |
*
|
jaroslav@557
|
166 |
* @return an Iterator over the elements in this set
|
jaroslav@557
|
167 |
* @see ConcurrentModificationException
|
jaroslav@557
|
168 |
*/
|
jaroslav@557
|
169 |
public Iterator<E> iterator() {
|
jaroslav@557
|
170 |
return map.keySet().iterator();
|
jaroslav@557
|
171 |
}
|
jaroslav@557
|
172 |
|
jaroslav@557
|
173 |
/**
|
jaroslav@557
|
174 |
* Returns the number of elements in this set (its cardinality).
|
jaroslav@557
|
175 |
*
|
jaroslav@557
|
176 |
* @return the number of elements in this set (its cardinality)
|
jaroslav@557
|
177 |
*/
|
jaroslav@557
|
178 |
public int size() {
|
jaroslav@557
|
179 |
return map.size();
|
jaroslav@557
|
180 |
}
|
jaroslav@557
|
181 |
|
jaroslav@557
|
182 |
/**
|
jaroslav@557
|
183 |
* Returns <tt>true</tt> if this set contains no elements.
|
jaroslav@557
|
184 |
*
|
jaroslav@557
|
185 |
* @return <tt>true</tt> if this set contains no elements
|
jaroslav@557
|
186 |
*/
|
jaroslav@557
|
187 |
public boolean isEmpty() {
|
jaroslav@557
|
188 |
return map.isEmpty();
|
jaroslav@557
|
189 |
}
|
jaroslav@557
|
190 |
|
jaroslav@557
|
191 |
/**
|
jaroslav@557
|
192 |
* Returns <tt>true</tt> if this set contains the specified element.
|
jaroslav@557
|
193 |
* More formally, returns <tt>true</tt> if and only if this set
|
jaroslav@557
|
194 |
* contains an element <tt>e</tt> such that
|
jaroslav@557
|
195 |
* <tt>(o==null ? e==null : o.equals(e))</tt>.
|
jaroslav@557
|
196 |
*
|
jaroslav@557
|
197 |
* @param o element whose presence in this set is to be tested
|
jaroslav@557
|
198 |
* @return <tt>true</tt> if this set contains the specified element
|
jaroslav@557
|
199 |
*/
|
jaroslav@557
|
200 |
public boolean contains(Object o) {
|
jaroslav@557
|
201 |
return map.containsKey(o);
|
jaroslav@557
|
202 |
}
|
jaroslav@557
|
203 |
|
jaroslav@557
|
204 |
/**
|
jaroslav@557
|
205 |
* Adds the specified element to this set if it is not already present.
|
jaroslav@557
|
206 |
* More formally, adds the specified element <tt>e</tt> to this set if
|
jaroslav@557
|
207 |
* this set contains no element <tt>e2</tt> such that
|
jaroslav@557
|
208 |
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
|
jaroslav@557
|
209 |
* If this set already contains the element, the call leaves the set
|
jaroslav@557
|
210 |
* unchanged and returns <tt>false</tt>.
|
jaroslav@557
|
211 |
*
|
jaroslav@557
|
212 |
* @param e element to be added to this set
|
jaroslav@557
|
213 |
* @return <tt>true</tt> if this set did not already contain the specified
|
jaroslav@557
|
214 |
* element
|
jaroslav@557
|
215 |
*/
|
jaroslav@557
|
216 |
public boolean add(E e) {
|
jaroslav@557
|
217 |
return map.put(e, PRESENT)==null;
|
jaroslav@557
|
218 |
}
|
jaroslav@557
|
219 |
|
jaroslav@557
|
220 |
/**
|
jaroslav@557
|
221 |
* Removes the specified element from this set if it is present.
|
jaroslav@557
|
222 |
* More formally, removes an element <tt>e</tt> such that
|
jaroslav@557
|
223 |
* <tt>(o==null ? e==null : o.equals(e))</tt>,
|
jaroslav@557
|
224 |
* if this set contains such an element. Returns <tt>true</tt> if
|
jaroslav@557
|
225 |
* this set contained the element (or equivalently, if this set
|
jaroslav@557
|
226 |
* changed as a result of the call). (This set will not contain the
|
jaroslav@557
|
227 |
* element once the call returns.)
|
jaroslav@557
|
228 |
*
|
jaroslav@557
|
229 |
* @param o object to be removed from this set, if present
|
jaroslav@557
|
230 |
* @return <tt>true</tt> if the set contained the specified element
|
jaroslav@557
|
231 |
*/
|
jaroslav@557
|
232 |
public boolean remove(Object o) {
|
jaroslav@557
|
233 |
return map.remove(o)==PRESENT;
|
jaroslav@557
|
234 |
}
|
jaroslav@557
|
235 |
|
jaroslav@557
|
236 |
/**
|
jaroslav@557
|
237 |
* Removes all of the elements from this set.
|
jaroslav@557
|
238 |
* The set will be empty after this call returns.
|
jaroslav@557
|
239 |
*/
|
jaroslav@557
|
240 |
public void clear() {
|
jaroslav@557
|
241 |
map.clear();
|
jaroslav@557
|
242 |
}
|
jaroslav@557
|
243 |
|
jaroslav@557
|
244 |
/**
|
jaroslav@557
|
245 |
* Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
|
jaroslav@557
|
246 |
* themselves are not cloned.
|
jaroslav@557
|
247 |
*
|
jaroslav@557
|
248 |
* @return a shallow copy of this set
|
jaroslav@557
|
249 |
*/
|
jaroslav@557
|
250 |
public Object clone() {
|
jaroslav@557
|
251 |
try {
|
jaroslav@557
|
252 |
HashSet<E> newSet = (HashSet<E>) super.clone();
|
jaroslav@557
|
253 |
newSet.map = (HashMap<E, Object>) map.clone();
|
jaroslav@557
|
254 |
return newSet;
|
jaroslav@557
|
255 |
} catch (CloneNotSupportedException e) {
|
jaroslav@557
|
256 |
throw new InternalError();
|
jaroslav@557
|
257 |
}
|
jaroslav@557
|
258 |
}
|
jaroslav@557
|
259 |
|
jaroslav@557
|
260 |
}
|