author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55391
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
20750 | 28 |
import java.io.InvalidObjectException; |
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49433
diff
changeset
|
29 |
import jdk.internal.access.SharedSecrets; |
20750 | 30 |
|
2 | 31 |
/** |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
32 |
* This class implements the {@code Set} interface, backed by a hash table |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
33 |
* (actually a {@code HashMap} instance). It makes no guarantees as to the |
2 | 34 |
* iteration order of the set; in particular, it does not guarantee that the |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
35 |
* order will remain constant over time. This class permits the {@code null} |
2 | 36 |
* element. |
37 |
* |
|
38 |
* <p>This class offers constant time performance for the basic operations |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
39 |
* ({@code add}, {@code remove}, {@code contains} and {@code size}), |
2 | 40 |
* assuming the hash function disperses the elements properly among the |
41 |
* buckets. Iterating over this set requires time proportional to the sum of |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
42 |
* the {@code HashSet} instance's size (the number of elements) plus the |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
43 |
* "capacity" of the backing {@code HashMap} instance (the number of |
2 | 44 |
* buckets). Thus, it's very important not to set the initial capacity too |
45 |
* high (or the load factor too low) if iteration performance is important. |
|
46 |
* |
|
47 |
* <p><strong>Note that this implementation is not synchronized.</strong> |
|
48 |
* If multiple threads access a hash set concurrently, and at least one of |
|
49 |
* the threads modifies the set, it <i>must</i> be synchronized externally. |
|
50 |
* This is typically accomplished by synchronizing on some object that |
|
51 |
* naturally encapsulates the set. |
|
52 |
* |
|
53 |
* If no such object exists, the set should be "wrapped" using the |
|
54 |
* {@link Collections#synchronizedSet Collections.synchronizedSet} |
|
55 |
* method. This is best done at creation time, to prevent accidental |
|
56 |
* unsynchronized access to the set:<pre> |
|
57 |
* Set s = Collections.synchronizedSet(new HashSet(...));</pre> |
|
58 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
59 |
* <p>The iterators returned by this class's {@code iterator} method are |
2 | 60 |
* <i>fail-fast</i>: if the set is modified at any time after the iterator is |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
61 |
* created, in any way except through the iterator's own {@code remove} |
2 | 62 |
* method, the Iterator throws a {@link ConcurrentModificationException}. |
63 |
* Thus, in the face of concurrent modification, the iterator fails quickly |
|
64 |
* and cleanly, rather than risking arbitrary, non-deterministic behavior at |
|
65 |
* an undetermined time in the future. |
|
66 |
* |
|
67 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
|
68 |
* as it is, generally speaking, impossible to make any hard guarantees in the |
|
69 |
* presence of unsynchronized concurrent modification. Fail-fast iterators |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
70 |
* throw {@code ConcurrentModificationException} on a best-effort basis. |
2 | 71 |
* Therefore, it would be wrong to write a program that depended on this |
72 |
* exception for its correctness: <i>the fail-fast behavior of iterators |
|
73 |
* should be used only to detect bugs.</i> |
|
74 |
* |
|
75 |
* <p>This class is a member of the |
|
49433
b6671a111395
8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents:
47423
diff
changeset
|
76 |
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework"> |
2 | 77 |
* Java Collections Framework</a>. |
78 |
* |
|
79 |
* @param <E> the type of elements maintained by this set |
|
80 |
* |
|
81 |
* @author Josh Bloch |
|
82 |
* @author Neal Gafter |
|
83 |
* @see Collection |
|
84 |
* @see Set |
|
85 |
* @see TreeSet |
|
86 |
* @see HashMap |
|
87 |
* @since 1.2 |
|
88 |
*/ |
|
89 |
||
90 |
public class HashSet<E> |
|
91 |
extends AbstractSet<E> |
|
92 |
implements Set<E>, Cloneable, java.io.Serializable |
|
93 |
{ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55391
diff
changeset
|
94 |
@java.io.Serial |
2 | 95 |
static final long serialVersionUID = -5024744406713321676L; |
96 |
||
97 |
private transient HashMap<E,Object> map; |
|
98 |
||
99 |
// Dummy value to associate with an Object in the backing Map |
|
100 |
private static final Object PRESENT = new Object(); |
|
101 |
||
102 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
103 |
* Constructs a new, empty set; the backing {@code HashMap} instance has |
2 | 104 |
* default initial capacity (16) and load factor (0.75). |
105 |
*/ |
|
106 |
public HashSet() { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
107 |
map = new HashMap<>(); |
2 | 108 |
} |
109 |
||
110 |
/** |
|
111 |
* Constructs a new set containing the elements in the specified |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
112 |
* collection. The {@code HashMap} is created with default load factor |
2 | 113 |
* (0.75) and an initial capacity sufficient to contain the elements in |
114 |
* the specified collection. |
|
115 |
* |
|
116 |
* @param c the collection whose elements are to be placed into this set |
|
117 |
* @throws NullPointerException if the specified collection is null |
|
118 |
*/ |
|
119 |
public HashSet(Collection<? extends E> c) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
120 |
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16)); |
2 | 121 |
addAll(c); |
122 |
} |
|
123 |
||
124 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
125 |
* Constructs a new, empty set; the backing {@code HashMap} instance has |
2 | 126 |
* the specified initial capacity and the specified load factor. |
127 |
* |
|
128 |
* @param initialCapacity the initial capacity of the hash map |
|
129 |
* @param loadFactor the load factor of the hash map |
|
130 |
* @throws IllegalArgumentException if the initial capacity is less |
|
131 |
* than zero, or if the load factor is nonpositive |
|
132 |
*/ |
|
133 |
public HashSet(int initialCapacity, float loadFactor) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
134 |
map = new HashMap<>(initialCapacity, loadFactor); |
2 | 135 |
} |
136 |
||
137 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
138 |
* Constructs a new, empty set; the backing {@code HashMap} instance has |
2 | 139 |
* the specified initial capacity and default load factor (0.75). |
140 |
* |
|
141 |
* @param initialCapacity the initial capacity of the hash table |
|
142 |
* @throws IllegalArgumentException if the initial capacity is less |
|
143 |
* than zero |
|
144 |
*/ |
|
145 |
public HashSet(int initialCapacity) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
146 |
map = new HashMap<>(initialCapacity); |
2 | 147 |
} |
148 |
||
149 |
/** |
|
150 |
* Constructs a new, empty linked hash set. (This package private |
|
151 |
* constructor is only used by LinkedHashSet.) The backing |
|
152 |
* HashMap instance is a LinkedHashMap with the specified initial |
|
153 |
* capacity and the specified load factor. |
|
154 |
* |
|
155 |
* @param initialCapacity the initial capacity of the hash map |
|
156 |
* @param loadFactor the load factor of the hash map |
|
157 |
* @param dummy ignored (distinguishes this |
|
158 |
* constructor from other int, float constructor.) |
|
159 |
* @throws IllegalArgumentException if the initial capacity is less |
|
160 |
* than zero, or if the load factor is nonpositive |
|
161 |
*/ |
|
162 |
HashSet(int initialCapacity, float loadFactor, boolean dummy) { |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
163 |
map = new LinkedHashMap<>(initialCapacity, loadFactor); |
2 | 164 |
} |
165 |
||
166 |
/** |
|
167 |
* Returns an iterator over the elements in this set. The elements |
|
168 |
* are returned in no particular order. |
|
169 |
* |
|
170 |
* @return an Iterator over the elements in this set |
|
171 |
* @see ConcurrentModificationException |
|
172 |
*/ |
|
173 |
public Iterator<E> iterator() { |
|
174 |
return map.keySet().iterator(); |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Returns the number of elements in this set (its cardinality). |
|
179 |
* |
|
180 |
* @return the number of elements in this set (its cardinality) |
|
181 |
*/ |
|
182 |
public int size() { |
|
183 |
return map.size(); |
|
184 |
} |
|
185 |
||
186 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
187 |
* Returns {@code true} if this set contains no elements. |
2 | 188 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
189 |
* @return {@code true} if this set contains no elements |
2 | 190 |
*/ |
191 |
public boolean isEmpty() { |
|
192 |
return map.isEmpty(); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
196 |
* Returns {@code true} if this set contains the specified element. |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
197 |
* More formally, returns {@code true} if and only if this set |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
198 |
* contains an element {@code e} such that |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
199 |
* {@code Objects.equals(o, e)}. |
2 | 200 |
* |
201 |
* @param o element whose presence in this set is to be tested |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
202 |
* @return {@code true} if this set contains the specified element |
2 | 203 |
*/ |
204 |
public boolean contains(Object o) { |
|
205 |
return map.containsKey(o); |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Adds the specified element to this set if it is not already present. |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
210 |
* More formally, adds the specified element {@code e} to this set if |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
211 |
* this set contains no element {@code e2} such that |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
212 |
* {@code Objects.equals(e, e2)}. |
2 | 213 |
* If this set already contains the element, the call leaves the set |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
214 |
* unchanged and returns {@code false}. |
2 | 215 |
* |
216 |
* @param e element to be added to this set |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
217 |
* @return {@code true} if this set did not already contain the specified |
2 | 218 |
* element |
219 |
*/ |
|
220 |
public boolean add(E e) { |
|
221 |
return map.put(e, PRESENT)==null; |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Removes the specified element from this set if it is present. |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
226 |
* More formally, removes an element {@code e} such that |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
227 |
* {@code Objects.equals(o, e)}, |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
228 |
* if this set contains such an element. Returns {@code true} if |
2 | 229 |
* this set contained the element (or equivalently, if this set |
230 |
* changed as a result of the call). (This set will not contain the |
|
231 |
* element once the call returns.) |
|
232 |
* |
|
233 |
* @param o object to be removed from this set, if present |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
234 |
* @return {@code true} if the set contained the specified element |
2 | 235 |
*/ |
236 |
public boolean remove(Object o) { |
|
237 |
return map.remove(o)==PRESENT; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Removes all of the elements from this set. |
|
242 |
* The set will be empty after this call returns. |
|
243 |
*/ |
|
244 |
public void clear() { |
|
245 |
map.clear(); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
249 |
* Returns a shallow copy of this {@code HashSet} instance: the elements |
2 | 250 |
* themselves are not cloned. |
251 |
* |
|
252 |
* @return a shallow copy of this set |
|
253 |
*/ |
|
12448 | 254 |
@SuppressWarnings("unchecked") |
2 | 255 |
public Object clone() { |
256 |
try { |
|
257 |
HashSet<E> newSet = (HashSet<E>) super.clone(); |
|
258 |
newSet.map = (HashMap<E, Object>) map.clone(); |
|
259 |
return newSet; |
|
260 |
} catch (CloneNotSupportedException e) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
9035
diff
changeset
|
261 |
throw new InternalError(e); |
2 | 262 |
} |
263 |
} |
|
264 |
||
265 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
266 |
* Save the state of this {@code HashSet} instance to a stream (that is, |
2 | 267 |
* serialize it). |
268 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
269 |
* @serialData The capacity of the backing {@code HashMap} instance |
2 | 270 |
* (int), and its load factor (float) are emitted, followed by |
271 |
* the size of the set (the number of elements it contains) |
|
272 |
* (int), followed by all of its elements (each an Object) in |
|
273 |
* no particular order. |
|
274 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55391
diff
changeset
|
275 |
@java.io.Serial |
2 | 276 |
private void writeObject(java.io.ObjectOutputStream s) |
277 |
throws java.io.IOException { |
|
278 |
// Write out any hidden serialization magic |
|
279 |
s.defaultWriteObject(); |
|
280 |
||
281 |
// Write out HashMap capacity and load factor |
|
282 |
s.writeInt(map.capacity()); |
|
283 |
s.writeFloat(map.loadFactor()); |
|
284 |
||
285 |
// Write out size |
|
286 |
s.writeInt(map.size()); |
|
287 |
||
288 |
// Write out all elements in the proper order. |
|
4110 | 289 |
for (E e : map.keySet()) |
290 |
s.writeObject(e); |
|
2 | 291 |
} |
292 |
||
293 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
25859
diff
changeset
|
294 |
* Reconstitute the {@code HashSet} instance from a stream (that is, |
2 | 295 |
* deserialize it). |
296 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55391
diff
changeset
|
297 |
@java.io.Serial |
2 | 298 |
private void readObject(java.io.ObjectInputStream s) |
299 |
throws java.io.IOException, ClassNotFoundException { |
|
300 |
// Read in any hidden serialization magic |
|
301 |
s.defaultReadObject(); |
|
302 |
||
20750 | 303 |
// Read capacity and verify non-negative. |
2 | 304 |
int capacity = s.readInt(); |
20750 | 305 |
if (capacity < 0) { |
306 |
throw new InvalidObjectException("Illegal capacity: " + |
|
307 |
capacity); |
|
308 |
} |
|
309 |
||
310 |
// Read load factor and verify positive and non NaN. |
|
2 | 311 |
float loadFactor = s.readFloat(); |
20750 | 312 |
if (loadFactor <= 0 || Float.isNaN(loadFactor)) { |
313 |
throw new InvalidObjectException("Illegal load factor: " + |
|
314 |
loadFactor); |
|
315 |
} |
|
316 |
||
317 |
// Read size and verify non-negative. |
|
318 |
int size = s.readInt(); |
|
319 |
if (size < 0) { |
|
320 |
throw new InvalidObjectException("Illegal size: " + |
|
321 |
size); |
|
322 |
} |
|
323 |
||
324 |
// Set the capacity according to the size and load factor ensuring that |
|
325 |
// the HashMap is at least 25% full but clamping to maximum capacity. |
|
326 |
capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f), |
|
327 |
HashMap.MAXIMUM_CAPACITY); |
|
328 |
||
47423 | 329 |
// Constructing the backing map will lazily create an array when the first element is |
330 |
// added, so check it before construction. Call HashMap.tableSizeFor to compute the |
|
331 |
// actual allocation size. Check Map.Entry[].class since it's the nearest public type to |
|
332 |
// what is actually created. |
|
333 |
SharedSecrets.getJavaObjectInputStreamAccess() |
|
334 |
.checkArray(s, Map.Entry[].class, HashMap.tableSizeFor(capacity)); |
|
335 |
||
20750 | 336 |
// Create backing HashMap |
12448 | 337 |
map = (((HashSet<?>)this) instanceof LinkedHashSet ? |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
20750
diff
changeset
|
338 |
new LinkedHashMap<>(capacity, loadFactor) : |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
20750
diff
changeset
|
339 |
new HashMap<>(capacity, loadFactor)); |
2 | 340 |
|
341 |
// Read in all elements in the proper order. |
|
342 |
for (int i=0; i<size; i++) { |
|
12448 | 343 |
@SuppressWarnings("unchecked") |
344 |
E e = (E) s.readObject(); |
|
2 | 345 |
map.put(e, PRESENT); |
346 |
} |
|
347 |
} |
|
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
348 |
|
19435
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
349 |
/** |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
350 |
* Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
351 |
* and <em>fail-fast</em> {@link Spliterator} over the elements in this |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
352 |
* set. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
353 |
* |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
354 |
* <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
355 |
* {@link Spliterator#DISTINCT}. Overriding implementations should document |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
356 |
* the reporting of additional characteristic values. |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
357 |
* |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
358 |
* @return a {@code Spliterator} over the elements in this set |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
359 |
* @since 1.8 |
9d7530ff42cb
8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents:
17168
diff
changeset
|
360 |
*/ |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
361 |
public Spliterator<E> spliterator() { |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
20750
diff
changeset
|
362 |
return new HashMap.KeySpliterator<>(map, 0, -1, 0, 0); |
17168
b7d3500f2516
8011426: java.util collection Spliterator implementations
psandoz
parents:
14342
diff
changeset
|
363 |
} |
55391
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
364 |
|
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
365 |
@Override |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
366 |
public Object[] toArray() { |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
367 |
return map.keysToArray(new Object[map.size()]); |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
368 |
} |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
369 |
|
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
370 |
@Override |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
371 |
public <T> T[] toArray(T[] a) { |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
372 |
return map.keysToArray(map.prepareArray(a)); |
1afe0cb93482
8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents:
52427
diff
changeset
|
373 |
} |
2 | 374 |
} |