ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.92 by dl, Sat Dec 2 20:55:01 2006 UTC vs.
Revision 1.320 by jsr166, Sun Sep 8 01:11:03 2019 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   package java.util.concurrent;
8 < import java.util.concurrent.locks.*;
9 < import java.util.*;
8 >
9 > import java.io.ObjectStreamField;
10   import java.io.Serializable;
11 < import java.io.IOException;
12 < import java.io.ObjectInputStream;
13 < import java.io.ObjectOutputStream;
11 > import java.lang.reflect.ParameterizedType;
12 > import java.lang.reflect.Type;
13 > import java.util.AbstractMap;
14 > import java.util.Arrays;
15 > import java.util.Collection;
16 > import java.util.Enumeration;
17 > import java.util.HashMap;
18 > import java.util.Hashtable;
19 > import java.util.Iterator;
20 > import java.util.Map;
21 > import java.util.NoSuchElementException;
22 > import java.util.Set;
23 > import java.util.Spliterator;
24 > import java.util.concurrent.atomic.AtomicReference;
25 > import java.util.concurrent.locks.LockSupport;
26 > import java.util.concurrent.locks.ReentrantLock;
27 > import java.util.function.BiConsumer;
28 > import java.util.function.BiFunction;
29 > import java.util.function.Consumer;
30 > import java.util.function.DoubleBinaryOperator;
31 > import java.util.function.Function;
32 > import java.util.function.IntBinaryOperator;
33 > import java.util.function.LongBinaryOperator;
34 > import java.util.function.Predicate;
35 > import java.util.function.ToDoubleBiFunction;
36 > import java.util.function.ToDoubleFunction;
37 > import java.util.function.ToIntBiFunction;
38 > import java.util.function.ToIntFunction;
39 > import java.util.function.ToLongBiFunction;
40 > import java.util.function.ToLongFunction;
41 > import java.util.stream.Stream;
42 > import jdk.internal.misc.Unsafe;
43  
44   /**
45   * A hash table supporting full concurrency of retrievals and
46 < * adjustable expected concurrency for updates. This class obeys the
46 > * high expected concurrency for updates. This class obeys the
47   * same functional specification as {@link java.util.Hashtable}, and
48   * includes versions of methods corresponding to each method of
49 < * <tt>Hashtable</tt>. However, even though all operations are
49 > * {@code Hashtable}. However, even though all operations are
50   * thread-safe, retrieval operations do <em>not</em> entail locking,
51   * and there is <em>not</em> any support for locking the entire table
52   * in a way that prevents all access.  This class is fully
53 < * interoperable with <tt>Hashtable</tt> in programs that rely on its
53 > * interoperable with {@code Hashtable} in programs that rely on its
54   * thread safety but not on its synchronization details.
55   *
56 < * <p> Retrieval operations (including <tt>get</tt>) generally do not
57 < * block, so may overlap with update operations (including
58 < * <tt>put</tt> and <tt>remove</tt>). Retrievals reflect the results
59 < * of the most recently <em>completed</em> update operations holding
60 < * upon their onset.  For aggregate operations such as <tt>putAll</tt>
61 < * and <tt>clear</tt>, concurrent retrievals may reflect insertion or
62 < * removal of only some entries.  Similarly, Iterators and
63 < * Enumerations return elements reflecting the state of the hash table
64 < * at some point at or since the creation of the iterator/enumeration.
65 < * They do <em>not</em> throw {@link ConcurrentModificationException}.
56 > * <p>Retrieval operations (including {@code get}) generally do not
57 > * block, so may overlap with update operations (including {@code put}
58 > * and {@code remove}). Retrievals reflect the results of the most
59 > * recently <em>completed</em> update operations holding upon their
60 > * onset. (More formally, an update operation for a given key bears a
61 > * <em>happens-before</em> relation with any (non-null) retrieval for
62 > * that key reporting the updated value.)  For aggregate operations
63 > * such as {@code putAll} and {@code clear}, concurrent retrievals may
64 > * reflect insertion or removal of only some entries.  Similarly,
65 > * Iterators, Spliterators and Enumerations return elements reflecting the
66 > * state of the hash table at some point at or since the creation of the
67 > * iterator/enumeration.  They do <em>not</em> throw {@link
68 > * java.util.ConcurrentModificationException ConcurrentModificationException}.
69   * However, iterators are designed to be used by only one thread at a time.
70 + * Bear in mind that the results of aggregate status methods including
71 + * {@code size}, {@code isEmpty}, and {@code containsValue} are typically
72 + * useful only when a map is not undergoing concurrent updates in other threads.
73 + * Otherwise the results of these methods reflect transient states
74 + * that may be adequate for monitoring or estimation purposes, but not
75 + * for program control.
76 + *
77 + * <p>The table is dynamically expanded when there are too many
78 + * collisions (i.e., keys that have distinct hash codes but fall into
79 + * the same slot modulo the table size), with the expected average
80 + * effect of maintaining roughly two bins per mapping (corresponding
81 + * to a 0.75 load factor threshold for resizing). There may be much
82 + * variance around this average as mappings are added and removed, but
83 + * overall, this maintains a commonly accepted time/space tradeoff for
84 + * hash tables.  However, resizing this or any other kind of hash
85 + * table may be a relatively slow operation. When possible, it is a
86 + * good idea to provide a size estimate as an optional {@code
87 + * initialCapacity} constructor argument. An additional optional
88 + * {@code loadFactor} constructor argument provides a further means of
89 + * customizing initial table capacity by specifying the table density
90 + * to be used in calculating the amount of space to allocate for the
91 + * given number of elements.  Also, for compatibility with previous
92 + * versions of this class, constructors may optionally specify an
93 + * expected {@code concurrencyLevel} as an additional hint for
94 + * internal sizing.  Note that using many keys with exactly the same
95 + * {@code hashCode()} is a sure way to slow down performance of any
96 + * hash table. To ameliorate impact, when keys are {@link Comparable},
97 + * this class may use comparison order among keys to help break ties.
98 + *
99 + * <p>A {@link Set} projection of a ConcurrentHashMap may be created
100 + * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
101 + * (using {@link #keySet(Object)} when only keys are of interest, and the
102 + * mapped values are (perhaps transiently) not used or all take the
103 + * same mapping value.
104   *
105 < * <p> The allowed concurrency among update operations is guided by
106 < * the optional <tt>concurrencyLevel</tt> constructor argument
107 < * (default <tt>16</tt>), which is used as a hint for internal sizing.  The
108 < * table is internally partitioned to try to permit the indicated
109 < * number of concurrent updates without contention. Because placement
110 < * in hash tables is essentially random, the actual concurrency will
45 < * vary.  Ideally, you should choose a value to accommodate as many
46 < * threads as will ever concurrently modify the table. Using a
47 < * significantly higher value than you need can waste space and time,
48 < * and a significantly lower value can lead to thread contention. But
49 < * overestimates and underestimates within an order of magnitude do
50 < * not usually have much noticeable impact. A value of one is
51 < * appropriate when it is known that only one thread will modify and
52 < * all others will only read. Also, resizing this or any other kind of
53 < * hash table is a relatively slow operation, so, when possible, it is
54 < * a good idea to provide estimates of expected table sizes in
55 < * constructors.
105 > * <p>A ConcurrentHashMap can be used as a scalable frequency map (a
106 > * form of histogram or multiset) by using {@link
107 > * java.util.concurrent.atomic.LongAdder} values and initializing via
108 > * {@link #computeIfAbsent computeIfAbsent}. For example, to add a count
109 > * to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use
110 > * {@code freqs.computeIfAbsent(key, k -> new LongAdder()).increment();}
111   *
112   * <p>This class and its views and iterators implement all of the
113   * <em>optional</em> methods of the {@link Map} and {@link Iterator}
114   * interfaces.
115   *
116 < * <p> Like {@link Hashtable} but unlike {@link HashMap}, this class
117 < * does <em>not</em> allow <tt>null</tt> to be used as a key or value.
116 > * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
117 > * does <em>not</em> allow {@code null} to be used as a key or value.
118 > *
119 > * <p>ConcurrentHashMaps support a set of sequential and parallel bulk
120 > * operations that, unlike most {@link Stream} methods, are designed
121 > * to be safely, and often sensibly, applied even with maps that are
122 > * being concurrently updated by other threads; for example, when
123 > * computing a snapshot summary of the values in a shared registry.
124 > * There are three kinds of operation, each with four forms, accepting
125 > * functions with keys, values, entries, and (key, value) pairs as
126 > * arguments and/or return values. Because the elements of a
127 > * ConcurrentHashMap are not ordered in any particular way, and may be
128 > * processed in different orders in different parallel executions, the
129 > * correctness of supplied functions should not depend on any
130 > * ordering, or on any other objects or values that may transiently
131 > * change while computation is in progress; and except for forEach
132 > * actions, should ideally be side-effect-free. Bulk operations on
133 > * {@link Map.Entry} objects do not support method {@code setValue}.
134 > *
135 > * <ul>
136 > * <li>forEach: Performs a given action on each element.
137 > * A variant form applies a given transformation on each element
138 > * before performing the action.
139 > *
140 > * <li>search: Returns the first available non-null result of
141 > * applying a given function on each element; skipping further
142 > * search when a result is found.
143 > *
144 > * <li>reduce: Accumulates each element.  The supplied reduction
145 > * function cannot rely on ordering (more formally, it should be
146 > * both associative and commutative).  There are five variants:
147 > *
148 > * <ul>
149 > *
150 > * <li>Plain reductions. (There is not a form of this method for
151 > * (key, value) function arguments since there is no corresponding
152 > * return type.)
153 > *
154 > * <li>Mapped reductions that accumulate the results of a given
155 > * function applied to each element.
156 > *
157 > * <li>Reductions to scalar doubles, longs, and ints, using a
158 > * given basis value.
159 > *
160 > * </ul>
161 > * </ul>
162 > *
163 > * <p>These bulk operations accept a {@code parallelismThreshold}
164 > * argument. Methods proceed sequentially if the current map size is
165 > * estimated to be less than the given threshold. Using a value of
166 > * {@code Long.MAX_VALUE} suppresses all parallelism.  Using a value
167 > * of {@code 1} results in maximal parallelism by partitioning into
168 > * enough subtasks to fully utilize the {@link
169 > * ForkJoinPool#commonPool()} that is used for all parallel
170 > * computations. Normally, you would initially choose one of these
171 > * extreme values, and then measure performance of using in-between
172 > * values that trade off overhead versus throughput.
173 > *
174 > * <p>The concurrency properties of bulk operations follow
175 > * from those of ConcurrentHashMap: Any non-null result returned
176 > * from {@code get(key)} and related access methods bears a
177 > * happens-before relation with the associated insertion or
178 > * update.  The result of any bulk operation reflects the
179 > * composition of these per-element relations (but is not
180 > * necessarily atomic with respect to the map as a whole unless it
181 > * is somehow known to be quiescent).  Conversely, because keys
182 > * and values in the map are never null, null serves as a reliable
183 > * atomic indicator of the current lack of any result.  To
184 > * maintain this property, null serves as an implicit basis for
185 > * all non-scalar reduction operations. For the double, long, and
186 > * int versions, the basis should be one that, when combined with
187 > * any other value, returns that other value (more formally, it
188 > * should be the identity element for the reduction). Most common
189 > * reductions have these properties; for example, computing a sum
190 > * with basis 0 or a minimum with basis MAX_VALUE.
191 > *
192 > * <p>Search and transformation functions provided as arguments
193 > * should similarly return null to indicate the lack of any result
194 > * (in which case it is not used). In the case of mapped
195 > * reductions, this also enables transformations to serve as
196 > * filters, returning null (or, in the case of primitive
197 > * specializations, the identity basis) if the element should not
198 > * be combined. You can create compound transformations and
199 > * filterings by composing them yourself under this "null means
200 > * there is nothing there now" rule before using them in search or
201 > * reduce operations.
202 > *
203 > * <p>Methods accepting and/or returning Entry arguments maintain
204 > * key-value associations. They may be useful for example when
205 > * finding the key for the greatest value. Note that "plain" Entry
206 > * arguments can be supplied using {@code new
207 > * AbstractMap.SimpleEntry(k,v)}.
208 > *
209 > * <p>Bulk operations may complete abruptly, throwing an
210 > * exception encountered in the application of a supplied
211 > * function. Bear in mind when handling such exceptions that other
212 > * concurrently executing functions could also have thrown
213 > * exceptions, or would have done so if the first exception had
214 > * not occurred.
215 > *
216 > * <p>Speedups for parallel compared to sequential forms are common
217 > * but not guaranteed.  Parallel operations involving brief functions
218 > * on small maps may execute more slowly than sequential forms if the
219 > * underlying work to parallelize the computation is more expensive
220 > * than the computation itself.  Similarly, parallelization may not
221 > * lead to much actual parallelism if all processors are busy
222 > * performing unrelated tasks.
223 > *
224 > * <p>All arguments to all task methods must be non-null.
225   *
226   * <p>This class is a member of the
227 < * <a href="{@docRoot}/../technotes/guides/collections/index.html">
227 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
228   * Java Collections Framework</a>.
229   *
230   * @since 1.5
# Line 70 | Line 232 | import java.io.ObjectOutputStream;
232   * @param <K> the type of keys maintained by this map
233   * @param <V> the type of mapped values
234   */
235 < public class ConcurrentHashMap<K, V> extends AbstractMap<K, V>
236 <        implements ConcurrentMap<K, V>, Serializable {
235 > public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
236 >    implements ConcurrentMap<K,V>, Serializable {
237      private static final long serialVersionUID = 7249069246763182397L;
238  
239      /*
240 <     * The basic strategy is to subdivide the table among Segments,
241 <     * each of which itself is a concurrently readable hash table.
240 >     * Overview:
241 >     *
242 >     * The primary design goal of this hash table is to maintain
243 >     * concurrent readability (typically method get(), but also
244 >     * iterators and related methods) while minimizing update
245 >     * contention. Secondary goals are to keep space consumption about
246 >     * the same or better than java.util.HashMap, and to support high
247 >     * initial insertion rates on an empty table by many threads.
248 >     *
249 >     * This map usually acts as a binned (bucketed) hash table.  Each
250 >     * key-value mapping is held in a Node.  Most nodes are instances
251 >     * of the basic Node class with hash, key, value, and next
252 >     * fields. However, various subclasses exist: TreeNodes are
253 >     * arranged in balanced trees, not lists.  TreeBins hold the roots
254 >     * of sets of TreeNodes. ForwardingNodes are placed at the heads
255 >     * of bins during resizing. ReservationNodes are used as
256 >     * placeholders while establishing values in computeIfAbsent and
257 >     * related methods.  The types TreeBin, ForwardingNode, and
258 >     * ReservationNode do not hold normal user keys, values, or
259 >     * hashes, and are readily distinguishable during search etc
260 >     * because they have negative hash fields and null key and value
261 >     * fields. (These special nodes are either uncommon or transient,
262 >     * so the impact of carrying around some unused fields is
263 >     * insignificant.)
264 >     *
265 >     * The table is lazily initialized to a power-of-two size upon the
266 >     * first insertion.  Each bin in the table normally contains a
267 >     * list of Nodes (most often, the list has only zero or one Node).
268 >     * Table accesses require volatile/atomic reads, writes, and
269 >     * CASes.  Because there is no other way to arrange this without
270 >     * adding further indirections, we use intrinsics
271 >     * (jdk.internal.misc.Unsafe) operations.
272 >     *
273 >     * We use the top (sign) bit of Node hash fields for control
274 >     * purposes -- it is available anyway because of addressing
275 >     * constraints.  Nodes with negative hash fields are specially
276 >     * handled or ignored in map methods.
277 >     *
278 >     * Insertion (via put or its variants) of the first node in an
279 >     * empty bin is performed by just CASing it to the bin.  This is
280 >     * by far the most common case for put operations under most
281 >     * key/hash distributions.  Other update operations (insert,
282 >     * delete, and replace) require locks.  We do not want to waste
283 >     * the space required to associate a distinct lock object with
284 >     * each bin, so instead use the first node of a bin list itself as
285 >     * a lock. Locking support for these locks relies on builtin
286 >     * "synchronized" monitors.
287 >     *
288 >     * Using the first node of a list as a lock does not by itself
289 >     * suffice though: When a node is locked, any update must first
290 >     * validate that it is still the first node after locking it, and
291 >     * retry if not. Because new nodes are always appended to lists,
292 >     * once a node is first in a bin, it remains first until deleted
293 >     * or the bin becomes invalidated (upon resizing).
294 >     *
295 >     * The main disadvantage of per-bin locks is that other update
296 >     * operations on other nodes in a bin list protected by the same
297 >     * lock can stall, for example when user equals() or mapping
298 >     * functions take a long time.  However, statistically, under
299 >     * random hash codes, this is not a common problem.  Ideally, the
300 >     * frequency of nodes in bins follows a Poisson distribution
301 >     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
302 >     * parameter of about 0.5 on average, given the resizing threshold
303 >     * of 0.75, although with a large variance because of resizing
304 >     * granularity. Ignoring variance, the expected occurrences of
305 >     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
306 >     * first values are:
307 >     *
308 >     * 0:    0.60653066
309 >     * 1:    0.30326533
310 >     * 2:    0.07581633
311 >     * 3:    0.01263606
312 >     * 4:    0.00157952
313 >     * 5:    0.00015795
314 >     * 6:    0.00001316
315 >     * 7:    0.00000094
316 >     * 8:    0.00000006
317 >     * more: less than 1 in ten million
318 >     *
319 >     * Lock contention probability for two threads accessing distinct
320 >     * elements is roughly 1 / (8 * #elements) under random hashes.
321 >     *
322 >     * Actual hash code distributions encountered in practice
323 >     * sometimes deviate significantly from uniform randomness.  This
324 >     * includes the case when N > (1<<30), so some keys MUST collide.
325 >     * Similarly for dumb or hostile usages in which multiple keys are
326 >     * designed to have identical hash codes or ones that differs only
327 >     * in masked-out high bits. So we use a secondary strategy that
328 >     * applies when the number of nodes in a bin exceeds a
329 >     * threshold. These TreeBins use a balanced tree to hold nodes (a
330 >     * specialized form of red-black trees), bounding search time to
331 >     * O(log N).  Each search step in a TreeBin is at least twice as
332 >     * slow as in a regular list, but given that N cannot exceed
333 >     * (1<<64) (before running out of addresses) this bounds search
334 >     * steps, lock hold times, etc, to reasonable constants (roughly
335 >     * 100 nodes inspected per operation worst case) so long as keys
336 >     * are Comparable (which is very common -- String, Long, etc).
337 >     * TreeBin nodes (TreeNodes) also maintain the same "next"
338 >     * traversal pointers as regular nodes, so can be traversed in
339 >     * iterators in the same way.
340 >     *
341 >     * The table is resized when occupancy exceeds a percentage
342 >     * threshold (nominally, 0.75, but see below).  Any thread
343 >     * noticing an overfull bin may assist in resizing after the
344 >     * initiating thread allocates and sets up the replacement array.
345 >     * However, rather than stalling, these other threads may proceed
346 >     * with insertions etc.  The use of TreeBins shields us from the
347 >     * worst case effects of overfilling while resizes are in
348 >     * progress.  Resizing proceeds by transferring bins, one by one,
349 >     * from the table to the next table. However, threads claim small
350 >     * blocks of indices to transfer (via field transferIndex) before
351 >     * doing so, reducing contention.  A generation stamp in field
352 >     * sizeCtl ensures that resizings do not overlap. Because we are
353 >     * using power-of-two expansion, the elements from each bin must
354 >     * either stay at same index, or move with a power of two
355 >     * offset. We eliminate unnecessary node creation by catching
356 >     * cases where old nodes can be reused because their next fields
357 >     * won't change.  On average, only about one-sixth of them need
358 >     * cloning when a table doubles. The nodes they replace will be
359 >     * garbage collectible as soon as they are no longer referenced by
360 >     * any reader thread that may be in the midst of concurrently
361 >     * traversing table.  Upon transfer, the old table bin contains
362 >     * only a special forwarding node (with hash field "MOVED") that
363 >     * contains the next table as its key. On encountering a
364 >     * forwarding node, access and update operations restart, using
365 >     * the new table.
366 >     *
367 >     * Each bin transfer requires its bin lock, which can stall
368 >     * waiting for locks while resizing. However, because other
369 >     * threads can join in and help resize rather than contend for
370 >     * locks, average aggregate waits become shorter as resizing
371 >     * progresses.  The transfer operation must also ensure that all
372 >     * accessible bins in both the old and new table are usable by any
373 >     * traversal.  This is arranged in part by proceeding from the
374 >     * last bin (table.length - 1) up towards the first.  Upon seeing
375 >     * a forwarding node, traversals (see class Traverser) arrange to
376 >     * move to the new table without revisiting nodes.  To ensure that
377 >     * no intervening nodes are skipped even when moved out of order,
378 >     * a stack (see class TableStack) is created on first encounter of
379 >     * a forwarding node during a traversal, to maintain its place if
380 >     * later processing the current table. The need for these
381 >     * save/restore mechanics is relatively rare, but when one
382 >     * forwarding node is encountered, typically many more will be.
383 >     * So Traversers use a simple caching scheme to avoid creating so
384 >     * many new TableStack nodes. (Thanks to Peter Levart for
385 >     * suggesting use of a stack here.)
386 >     *
387 >     * The traversal scheme also applies to partial traversals of
388 >     * ranges of bins (via an alternate Traverser constructor)
389 >     * to support partitioned aggregate operations.  Also, read-only
390 >     * operations give up if ever forwarded to a null table, which
391 >     * provides support for shutdown-style clearing, which is also not
392 >     * currently implemented.
393 >     *
394 >     * Lazy table initialization minimizes footprint until first use,
395 >     * and also avoids resizings when the first operation is from a
396 >     * putAll, constructor with map argument, or deserialization.
397 >     * These cases attempt to override the initial capacity settings,
398 >     * but harmlessly fail to take effect in cases of races.
399 >     *
400 >     * The element count is maintained using a specialization of
401 >     * LongAdder. We need to incorporate a specialization rather than
402 >     * just use a LongAdder in order to access implicit
403 >     * contention-sensing that leads to creation of multiple
404 >     * CounterCells.  The counter mechanics avoid contention on
405 >     * updates but can encounter cache thrashing if read too
406 >     * frequently during concurrent access. To avoid reading so often,
407 >     * resizing under contention is attempted only upon adding to a
408 >     * bin already holding two or more nodes. Under uniform hash
409 >     * distributions, the probability of this occurring at threshold
410 >     * is around 13%, meaning that only about 1 in 8 puts check
411 >     * threshold (and after resizing, many fewer do so).
412 >     *
413 >     * TreeBins use a special form of comparison for search and
414 >     * related operations (which is the main reason we cannot use
415 >     * existing collections such as TreeMaps). TreeBins contain
416 >     * Comparable elements, but may contain others, as well as
417 >     * elements that are Comparable but not necessarily Comparable for
418 >     * the same T, so we cannot invoke compareTo among them. To handle
419 >     * this, the tree is ordered primarily by hash value, then by
420 >     * Comparable.compareTo order if applicable.  On lookup at a node,
421 >     * if elements are not comparable or compare as 0 then both left
422 >     * and right children may need to be searched in the case of tied
423 >     * hash values. (This corresponds to the full list search that
424 >     * would be necessary if all elements were non-Comparable and had
425 >     * tied hashes.) On insertion, to keep a total ordering (or as
426 >     * close as is required here) across rebalancings, we compare
427 >     * classes and identityHashCodes as tie-breakers. The red-black
428 >     * balancing code is updated from pre-jdk-collections
429 >     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
430 >     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
431 >     * Algorithms" (CLR).
432 >     *
433 >     * TreeBins also require an additional locking mechanism.  While
434 >     * list traversal is always possible by readers even during
435 >     * updates, tree traversal is not, mainly because of tree-rotations
436 >     * that may change the root node and/or its linkages.  TreeBins
437 >     * include a simple read-write lock mechanism parasitic on the
438 >     * main bin-synchronization strategy: Structural adjustments
439 >     * associated with an insertion or removal are already bin-locked
440 >     * (and so cannot conflict with other writers) but must wait for
441 >     * ongoing readers to finish. Since there can be only one such
442 >     * waiter, we use a simple scheme using a single "waiter" field to
443 >     * block writers.  However, readers need never block.  If the root
444 >     * lock is held, they proceed along the slow traversal path (via
445 >     * next-pointers) until the lock becomes available or the list is
446 >     * exhausted, whichever comes first. These cases are not fast, but
447 >     * maximize aggregate expected throughput.
448 >     *
449 >     * Maintaining API and serialization compatibility with previous
450 >     * versions of this class introduces several oddities. Mainly: We
451 >     * leave untouched but unused constructor arguments referring to
452 >     * concurrencyLevel. We accept a loadFactor constructor argument,
453 >     * but apply it only to initial table capacity (which is the only
454 >     * time that we can guarantee to honor it.) We also declare an
455 >     * unused "Segment" class that is instantiated in minimal form
456 >     * only when serializing.
457 >     *
458 >     * Also, solely for compatibility with previous versions of this
459 >     * class, it extends AbstractMap, even though all of its methods
460 >     * are overridden, so it is just useless baggage.
461 >     *
462 >     * This file is organized to make things a little easier to follow
463 >     * while reading than they might otherwise: First the main static
464 >     * declarations and utilities, then fields, then main public
465 >     * methods (with a few factorings of multiple public methods into
466 >     * internal ones), then sizing methods, trees, traversers, and
467 >     * bulk operations.
468       */
469  
470      /* ---------------- Constants -------------- */
471  
472      /**
473 <     * The default initial capacity for this table,
474 <     * used when not otherwise specified in a constructor.
473 >     * The largest possible table capacity.  This value must be
474 >     * exactly 1<<30 to stay within Java array allocation and indexing
475 >     * bounds for power of two table sizes, and is further required
476 >     * because the top two bits of 32bit hash fields are used for
477 >     * control purposes.
478       */
479 <    static final int DEFAULT_INITIAL_CAPACITY = 16;
479 >    private static final int MAXIMUM_CAPACITY = 1 << 30;
480  
481      /**
482 <     * The default load factor for this table, used when not
483 <     * otherwise specified in a constructor.
482 >     * The default initial table capacity.  Must be a power of 2
483 >     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
484       */
485 <    static final float DEFAULT_LOAD_FACTOR = 0.75f;
485 >    private static final int DEFAULT_CAPACITY = 16;
486  
487      /**
488 <     * The default concurrency level for this table, used when not
489 <     * otherwise specified in a constructor.
488 >     * The largest possible (non-power of two) array size.
489 >     * Needed by toArray and related methods.
490       */
491 <    static final int DEFAULT_CONCURRENCY_LEVEL = 16;
491 >    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
492  
493      /**
494 <     * The maximum capacity, used if a higher value is implicitly
495 <     * specified by either of the constructors with arguments.  MUST
105 <     * be a power of two <= 1<<30 to ensure that entries are indexable
106 <     * using ints.
494 >     * The default concurrency level for this table. Unused but
495 >     * defined for compatibility with previous versions of this class.
496       */
497 <    static final int MAXIMUM_CAPACITY = 1 << 30;
497 >    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
498  
499      /**
500 <     * The maximum number of segments to allow; used to bound
501 <     * constructor arguments.
500 >     * The load factor for this table. Overrides of this value in
501 >     * constructors affect only the initial table capacity.  The
502 >     * actual floating point value isn't normally used -- it is
503 >     * simpler to use expressions such as {@code n - (n >>> 2)} for
504 >     * the associated resizing threshold.
505       */
506 <    static final int MAX_SEGMENTS = 1 << 16; // slightly conservative
506 >    private static final float LOAD_FACTOR = 0.75f;
507  
508      /**
509 <     * Number of unsynchronized retries in size and containsValue
510 <     * methods before resorting to locking. This is used to avoid
511 <     * unbounded retries if tables undergo continuous modification
512 <     * which would make it impossible to obtain an accurate result.
509 >     * The bin count threshold for using a tree rather than list for a
510 >     * bin.  Bins are converted to trees when adding an element to a
511 >     * bin with at least this many nodes. The value must be greater
512 >     * than 2, and should be at least 8 to mesh with assumptions in
513 >     * tree removal about conversion back to plain bins upon
514 >     * shrinkage.
515       */
516 <    static final int RETRIES_BEFORE_LOCK = 2;
123 <
124 <    /* ---------------- Fields -------------- */
516 >    static final int TREEIFY_THRESHOLD = 8;
517  
518      /**
519 <     * Mask value for indexing into segments. The upper bits of a
520 <     * key's hash code are used to choose the segment.
519 >     * The bin count threshold for untreeifying a (split) bin during a
520 >     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
521 >     * most 6 to mesh with shrinkage detection under removal.
522       */
523 <    final int segmentMask;
523 >    static final int UNTREEIFY_THRESHOLD = 6;
524  
525      /**
526 <     * Shift value for indexing within segments.
526 >     * The smallest table capacity for which bins may be treeified.
527 >     * (Otherwise the table is resized if too many nodes in a bin.)
528 >     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
529 >     * conflicts between resizing and treeification thresholds.
530       */
531 <    final int segmentShift;
531 >    static final int MIN_TREEIFY_CAPACITY = 64;
532  
533      /**
534 <     * The segments, each of which is a specialized hash table
534 >     * Minimum number of rebinnings per transfer step. Ranges are
535 >     * subdivided to allow multiple resizer threads.  This value
536 >     * serves as a lower bound to avoid resizers encountering
537 >     * excessive memory contention.  The value should be at least
538 >     * DEFAULT_CAPACITY.
539       */
540 <    final Segment<K,V>[] segments;
141 <
142 <    transient Set<K> keySet;
143 <    transient Set<Map.Entry<K,V>> entrySet;
144 <    transient Collection<V> values;
145 <
146 <    /* ---------------- Small Utilities -------------- */
540 >    private static final int MIN_TRANSFER_STRIDE = 16;
541  
542      /**
543 <     * Applies a supplemental hash function to a given hashCode, which
544 <     * defends against poor quality hash functions.  This is critical
151 <     * because ConcurrentHashMap uses power-of-two length hash tables,
152 <     * that otherwise encounter collisions for hashCodes that do not
153 <     * differ in lower bits.
543 >     * The number of bits used for generation stamp in sizeCtl.
544 >     * Must be at least 6 for 32bit arrays.
545       */
546 <    private static int hash(int h) {
156 <        // Spread bits to regularize both segment and index locations,
157 <        // using variant of Jenkins's shift-based hash.
158 <        h += ~(h << 13);
159 <        h ^= h >>> 7;
160 <        h += h << 3;
161 <        h ^= h >>> 17;
162 <        h += h << 5;
163 <        return h;
164 <    }
546 >    private static final int RESIZE_STAMP_BITS = 16;
547  
548      /**
549 <     * Returns the segment that should be used for key with given hash
550 <     * @param hash the hash code for the key
169 <     * @return the segment
549 >     * The maximum number of threads that can help resize.
550 >     * Must fit in 32 - RESIZE_STAMP_BITS bits.
551       */
552 <    final Segment<K,V> segmentFor(int hash) {
172 <        return segments[(hash >>> segmentShift) & segmentMask];
173 <    }
174 <
175 <    /* ---------------- Inner Classes -------------- */
552 >    private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
553  
554      /**
555 <     * ConcurrentHashMap list entry. Note that this is never exported
179 <     * out as a user-visible Map.Entry.
180 <     *
181 <     * Because the value field is volatile, not final, it is legal wrt
182 <     * the Java Memory Model for an unsynchronized reader to see null
183 <     * instead of initial value when read via a data race.  Although a
184 <     * reordering leading to this is not likely to ever actually
185 <     * occur, the Segment.readValueUnderLock method is used as a
186 <     * backup in case a null (pre-initialized) value is ever seen in
187 <     * an unsynchronized access method.
555 >     * The bit shift for recording size stamp in sizeCtl.
556       */
557 <    static final class HashEntry<K,V> {
190 <        final K key;
191 <        final int hash;
192 <        volatile V value;
193 <        final HashEntry<K,V> next;
194 <
195 <        HashEntry(K key, int hash, HashEntry<K,V> next, V value) {
196 <            this.key = key;
197 <            this.hash = hash;
198 <            this.next = next;
199 <            this.value = value;
200 <        }
201 <
202 <        @SuppressWarnings("unchecked")
203 <        static final <K,V> HashEntry<K,V>[] newArray(int i) {
204 <            return new HashEntry[i];
205 <        }
206 <    }
557 >    private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
558  
559 <    /**
560 <     * Segments are specialized versions of hash tables.  This
210 <     * subclasses from ReentrantLock opportunistically, just to
211 <     * simplify some locking and avoid separate construction.
559 >    /*
560 >     * Encodings for Node hash fields. See above for explanation.
561       */
562 <    static final class Segment<K,V> extends ReentrantLock implements Serializable {
563 <        /*
564 <         * Segments maintain a table of entry lists that are ALWAYS
565 <         * kept in a consistent state, so can be read without locking.
217 <         * Next fields of nodes are immutable (final).  All list
218 <         * additions are performed at the front of each bin. This
219 <         * makes it easy to check changes, and also fast to traverse.
220 <         * When nodes would otherwise be changed, new nodes are
221 <         * created to replace them. This works well for hash tables
222 <         * since the bin lists tend to be short. (The average length
223 <         * is less than two for the default load factor threshold.)
224 <         *
225 <         * Read operations can thus proceed without locking, but rely
226 <         * on selected uses of volatiles to ensure that completed
227 <         * write operations performed by other threads are
228 <         * noticed. For most purposes, the "count" field, tracking the
229 <         * number of elements, serves as that volatile variable
230 <         * ensuring visibility.  This is convenient because this field
231 <         * needs to be read in many read operations anyway:
232 <         *
233 <         *   - All (unsynchronized) read operations must first read the
234 <         *     "count" field, and should not look at table entries if
235 <         *     it is 0.
236 <         *
237 <         *   - All (synchronized) write operations should write to
238 <         *     the "count" field after structurally changing any bin.
239 <         *     The operations must not take any action that could even
240 <         *     momentarily cause a concurrent read operation to see
241 <         *     inconsistent data. This is made easier by the nature of
242 <         *     the read operations in Map. For example, no operation
243 <         *     can reveal that the table has grown but the threshold
244 <         *     has not yet been updated, so there are no atomicity
245 <         *     requirements for this with respect to reads.
246 <         *
247 <         * As a guide, all critical volatile reads and writes to the
248 <         * count field are marked in code comments.
249 <         */
250 <
251 <        private static final long serialVersionUID = 2249069246763182397L;
252 <
253 <        /**
254 <         * The number of elements in this segment's region.
255 <         */
256 <        transient volatile int count;
562 >    static final int MOVED     = -1; // hash for forwarding nodes
563 >    static final int TREEBIN   = -2; // hash for roots of trees
564 >    static final int RESERVED  = -3; // hash for transient reservations
565 >    static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
566  
567 <        /**
568 <         * Number of updates that alter the size of the table. This is
260 <         * used during bulk-read methods to make sure they see a
261 <         * consistent snapshot: If modCounts change during a traversal
262 <         * of segments computing size or checking containsValue, then
263 <         * we might have an inconsistent view of state so (usually)
264 <         * must retry.
265 <         */
266 <        transient int modCount;
567 >    /** Number of CPUS, to place bounds on some sizings */
568 >    static final int NCPU = Runtime.getRuntime().availableProcessors();
569  
570 <        /**
571 <         * The table is rehashed when its size exceeds this threshold.
572 <         * (The value of this field is always <tt>(int)(capacity *
573 <         * loadFactor)</tt>.)
574 <         */
575 <        transient int threshold;
570 >    /**
571 >     * Serialized pseudo-fields, provided only for jdk7 compatibility.
572 >     * @serialField segments Segment[]
573 >     *   The segments, each of which is a specialized hash table.
574 >     * @serialField segmentMask int
575 >     *   Mask value for indexing into segments. The upper bits of a
576 >     *   key's hash code are used to choose the segment.
577 >     * @serialField segmentShift int
578 >     *   Shift value for indexing within segments.
579 >     */
580 >    private static final ObjectStreamField[] serialPersistentFields = {
581 >        new ObjectStreamField("segments", Segment[].class),
582 >        new ObjectStreamField("segmentMask", Integer.TYPE),
583 >        new ObjectStreamField("segmentShift", Integer.TYPE),
584 >    };
585  
586 <        /**
276 <         * The per-segment table.
277 <         */
278 <        transient volatile HashEntry<K,V>[] table;
586 >    /* ---------------- Nodes -------------- */
587  
588 <        /**
589 <         * The load factor for the hash table.  Even though this value
590 <         * is same for all segments, it is replicated to avoid needing
591 <         * links to outer object.
592 <         * @serial
593 <         */
594 <        final float loadFactor;
595 <
596 <        Segment(int initialCapacity, float lf) {
597 <            loadFactor = lf;
598 <            setTable(HashEntry.<K,V>newArray(initialCapacity));
599 <        }
588 >    /**
589 >     * Key-value entry.  This class is never exported out as a
590 >     * user-mutable Map.Entry (i.e., one supporting setValue; see
591 >     * MapEntry below), but can be used for read-only traversals used
592 >     * in bulk tasks.  Subclasses of Node with a negative hash field
593 >     * are special, and contain null keys and values (but are never
594 >     * exported).  Otherwise, keys and vals are never null.
595 >     */
596 >    static class Node<K,V> implements Map.Entry<K,V> {
597 >        final int hash;
598 >        final K key;
599 >        volatile V val;
600 >        volatile Node<K,V> next;
601  
602 <        @SuppressWarnings("unchecked")
603 <        static final <K,V> Segment<K,V>[] newArray(int i) {
604 <            return new Segment[i];
602 >        Node(int hash, K key, V val) {
603 >            this.hash = hash;
604 >            this.key = key;
605 >            this.val = val;
606          }
607  
608 <        /**
609 <         * Sets table to new HashEntry array.
610 <         * Call only while holding lock or in constructor.
301 <         */
302 <        void setTable(HashEntry<K,V>[] newTable) {
303 <            threshold = (int)(newTable.length * loadFactor);
304 <            table = newTable;
608 >        Node(int hash, K key, V val, Node<K,V> next) {
609 >            this(hash, key, val);
610 >            this.next = next;
611          }
612  
613 <        /**
614 <         * Returns properly casted first entry of bin for given hash.
615 <         */
616 <        HashEntry<K,V> getFirst(int hash) {
617 <            HashEntry<K,V>[] tab = table;
618 <            return tab[hash & (tab.length - 1)];
613 >        public final K getKey()     { return key; }
614 >        public final V getValue()   { return val; }
615 >        public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
616 >        public final String toString() {
617 >            return Helpers.mapEntryToString(key, val);
618 >        }
619 >        public final V setValue(V value) {
620 >            throw new UnsupportedOperationException();
621 >        }
622 >
623 >        public final boolean equals(Object o) {
624 >            Object k, v, u; Map.Entry<?,?> e;
625 >            return ((o instanceof Map.Entry) &&
626 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
627 >                    (v = e.getValue()) != null &&
628 >                    (k == key || k.equals(key)) &&
629 >                    (v == (u = val) || v.equals(u)));
630          }
631  
632          /**
633 <         * Reads value field of an entry under lock. Called if value
317 <         * field ever appears to be null. This is possible only if a
318 <         * compiler happens to reorder a HashEntry initialization with
319 <         * its table assignment, which is legal under memory model
320 <         * but is not known to ever occur.
633 >         * Virtualized support for map.get(); overridden in subclasses.
634           */
635 <        V readValueUnderLock(HashEntry<K,V> e) {
636 <            lock();
637 <            try {
638 <                return e.value;
639 <            } finally {
640 <                unlock();
641 <            }
642 <        }
643 <
331 <        /* Specialized implementations of map methods */
332 <
333 <        V get(Object key, int hash) {
334 <            if (count != 0) { // read-volatile
335 <                HashEntry<K,V> e = getFirst(hash);
336 <                while (e != null) {
337 <                    if (e.hash == hash && key.equals(e.key)) {
338 <                        V v = e.value;
339 <                        if (v != null)
340 <                            return v;
341 <                        return readValueUnderLock(e); // recheck
342 <                    }
343 <                    e = e.next;
344 <                }
635 >        Node<K,V> find(int h, Object k) {
636 >            Node<K,V> e = this;
637 >            if (k != null) {
638 >                do {
639 >                    K ek;
640 >                    if (e.hash == h &&
641 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
642 >                        return e;
643 >                } while ((e = e.next) != null);
644              }
645              return null;
646          }
647 +    }
648  
649 <        boolean containsKey(Object key, int hash) {
350 <            if (count != 0) { // read-volatile
351 <                HashEntry<K,V> e = getFirst(hash);
352 <                while (e != null) {
353 <                    if (e.hash == hash && key.equals(e.key))
354 <                        return true;
355 <                    e = e.next;
356 <                }
357 <            }
358 <            return false;
359 <        }
649 >    /* ---------------- Static utilities -------------- */
650  
651 <        boolean containsValue(Object value) {
652 <            if (count != 0) { // read-volatile
653 <                HashEntry<K,V>[] tab = table;
654 <                int len = tab.length;
655 <                for (int i = 0 ; i < len; i++) {
656 <                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
657 <                        V v = e.value;
658 <                        if (v == null) // recheck
659 <                            v = readValueUnderLock(e);
660 <                        if (value.equals(v))
661 <                            return true;
662 <                    }
663 <                }
664 <            }
665 <            return false;
666 <        }
651 >    /**
652 >     * Spreads (XORs) higher bits of hash to lower and also forces top
653 >     * bit to 0. Because the table uses power-of-two masking, sets of
654 >     * hashes that vary only in bits above the current mask will
655 >     * always collide. (Among known examples are sets of Float keys
656 >     * holding consecutive whole numbers in small tables.)  So we
657 >     * apply a transform that spreads the impact of higher bits
658 >     * downward. There is a tradeoff between speed, utility, and
659 >     * quality of bit-spreading. Because many common sets of hashes
660 >     * are already reasonably distributed (so don't benefit from
661 >     * spreading), and because we use trees to handle large sets of
662 >     * collisions in bins, we just XOR some shifted bits in the
663 >     * cheapest possible way to reduce systematic lossage, as well as
664 >     * to incorporate impact of the highest bits that would otherwise
665 >     * never be used in index calculations because of table bounds.
666 >     */
667 >    static final int spread(int h) {
668 >        return (h ^ (h >>> 16)) & HASH_BITS;
669 >    }
670  
671 <        boolean replace(K key, int hash, V oldValue, V newValue) {
672 <            lock();
673 <            try {
674 <                HashEntry<K,V> e = getFirst(hash);
675 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
676 <                    e = e.next;
677 <
678 <                boolean replaced = false;
386 <                if (e != null && oldValue.equals(e.value)) {
387 <                    replaced = true;
388 <                    e.value = newValue;
389 <                }
390 <                return replaced;
391 <            } finally {
392 <                unlock();
393 <            }
394 <        }
671 >    /**
672 >     * Returns a power of two table size for the given desired capacity.
673 >     * See Hackers Delight, sec 3.2
674 >     */
675 >    private static final int tableSizeFor(int c) {
676 >        int n = -1 >>> Integer.numberOfLeadingZeros(c - 1);
677 >        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
678 >    }
679  
680 <        V replace(K key, int hash, V newValue) {
681 <            lock();
682 <            try {
683 <                HashEntry<K,V> e = getFirst(hash);
684 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
685 <                    e = e.next;
686 <
687 <                V oldValue = null;
688 <                if (e != null) {
689 <                    oldValue = e.value;
690 <                    e.value = newValue;
680 >    /**
681 >     * Returns x's Class if it is of the form "class C implements
682 >     * Comparable<C>", else null.
683 >     */
684 >    static Class<?> comparableClassFor(Object x) {
685 >        if (x instanceof Comparable) {
686 >            Class<?> c; Type[] ts, as; ParameterizedType p;
687 >            if ((c = x.getClass()) == String.class) // bypass checks
688 >                return c;
689 >            if ((ts = c.getGenericInterfaces()) != null) {
690 >                for (Type t : ts) {
691 >                    if ((t instanceof ParameterizedType) &&
692 >                        ((p = (ParameterizedType)t).getRawType() ==
693 >                         Comparable.class) &&
694 >                        (as = p.getActualTypeArguments()) != null &&
695 >                        as.length == 1 && as[0] == c) // type arg is c
696 >                        return c;
697                  }
408                return oldValue;
409            } finally {
410                unlock();
698              }
699          }
700 +        return null;
701 +    }
702  
703 +    /**
704 +     * Returns k.compareTo(x) if x matches kc (k's screened comparable
705 +     * class), else 0.
706 +     */
707 +    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
708 +    static int compareComparables(Class<?> kc, Object k, Object x) {
709 +        return (x == null || x.getClass() != kc ? 0 :
710 +                ((Comparable)k).compareTo(x));
711 +    }
712  
713 <        V put(K key, int hash, V value, boolean onlyIfAbsent) {
416 <            lock();
417 <            try {
418 <                int c = count;
419 <                if (c++ > threshold) // ensure capacity
420 <                    rehash();
421 <                HashEntry<K,V>[] tab = table;
422 <                int index = hash & (tab.length - 1);
423 <                HashEntry<K,V> first = tab[index];
424 <                HashEntry<K,V> e = first;
425 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
426 <                    e = e.next;
427 <
428 <                V oldValue;
429 <                if (e != null) {
430 <                    oldValue = e.value;
431 <                    if (!onlyIfAbsent)
432 <                        e.value = value;
433 <                }
434 <                else {
435 <                    oldValue = null;
436 <                    ++modCount;
437 <                    tab[index] = new HashEntry<K,V>(key, hash, first, value);
438 <                    count = c; // write-volatile
439 <                }
440 <                return oldValue;
441 <            } finally {
442 <                unlock();
443 <            }
444 <        }
713 >    /* ---------------- Table element access -------------- */
714  
715 <        void rehash() {
716 <            HashEntry<K,V>[] oldTable = table;
717 <            int oldCapacity = oldTable.length;
718 <            if (oldCapacity >= MAXIMUM_CAPACITY)
719 <                return;
715 >    /*
716 >     * Atomic access methods are used for table elements as well as
717 >     * elements of in-progress next table while resizing.  All uses of
718 >     * the tab arguments must be null checked by callers.  All callers
719 >     * also paranoically precheck that tab's length is not zero (or an
720 >     * equivalent check), thus ensuring that any index argument taking
721 >     * the form of a hash value anded with (length - 1) is a valid
722 >     * index.  Note that, to be correct wrt arbitrary concurrency
723 >     * errors by users, these checks must operate on local variables,
724 >     * which accounts for some odd-looking inline assignments below.
725 >     * Note that calls to setTabAt always occur within locked regions,
726 >     * and so require only release ordering.
727 >     */
728 >
729 >    @SuppressWarnings("unchecked")
730 >    static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
731 >        return (Node<K,V>)U.getObjectAcquire(tab, ((long)i << ASHIFT) + ABASE);
732 >    }
733  
734 <            /*
735 <             * Reclassify nodes in each list to new Map.  Because we are
736 <             * using power-of-two expansion, the elements from each bin
737 <             * must either stay at same index, or move with a power of two
456 <             * offset. We eliminate unnecessary node creation by catching
457 <             * cases where old nodes can be reused because their next
458 <             * fields won't change. Statistically, at the default
459 <             * threshold, only about one-sixth of them need cloning when
460 <             * a table doubles. The nodes they replace will be garbage
461 <             * collectable as soon as they are no longer referenced by any
462 <             * reader thread that may be in the midst of traversing table
463 <             * right now.
464 <             */
465 <
466 <            HashEntry<K,V>[] newTable = HashEntry.newArray(oldCapacity<<1);
467 <            threshold = (int)(newTable.length * loadFactor);
468 <            int sizeMask = newTable.length - 1;
469 <            for (int i = 0; i < oldCapacity ; i++) {
470 <                // We need to guarantee that any existing reads of old Map can
471 <                //  proceed. So we cannot yet null out each bin.
472 <                HashEntry<K,V> e = oldTable[i];
473 <
474 <                if (e != null) {
475 <                    HashEntry<K,V> next = e.next;
476 <                    int idx = e.hash & sizeMask;
477 <
478 <                    //  Single node on list
479 <                    if (next == null)
480 <                        newTable[idx] = e;
734 >    static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
735 >                                        Node<K,V> c, Node<K,V> v) {
736 >        return U.compareAndSetObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
737 >    }
738  
739 <                    else {
740 <                        // Reuse trailing consecutive sequence at same slot
741 <                        HashEntry<K,V> lastRun = e;
485 <                        int lastIdx = idx;
486 <                        for (HashEntry<K,V> last = next;
487 <                             last != null;
488 <                             last = last.next) {
489 <                            int k = last.hash & sizeMask;
490 <                            if (k != lastIdx) {
491 <                                lastIdx = k;
492 <                                lastRun = last;
493 <                            }
494 <                        }
495 <                        newTable[lastIdx] = lastRun;
739 >    static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
740 >        U.putObjectRelease(tab, ((long)i << ASHIFT) + ABASE, v);
741 >    }
742  
743 <                        // Clone all remaining nodes
498 <                        for (HashEntry<K,V> p = e; p != lastRun; p = p.next) {
499 <                            int k = p.hash & sizeMask;
500 <                            HashEntry<K,V> n = newTable[k];
501 <                            newTable[k] = new HashEntry<K,V>(p.key, p.hash,
502 <                                                             n, p.value);
503 <                        }
504 <                    }
505 <                }
506 <            }
507 <            table = newTable;
508 <        }
743 >    /* ---------------- Fields -------------- */
744  
745 <        /**
746 <         * Remove; match on key only if value null, else match both.
747 <         */
748 <        V remove(Object key, int hash, Object value) {
749 <            lock();
515 <            try {
516 <                int c = count - 1;
517 <                HashEntry<K,V>[] tab = table;
518 <                int index = hash & (tab.length - 1);
519 <                HashEntry<K,V> first = tab[index];
520 <                HashEntry<K,V> e = first;
521 <                while (e != null && (e.hash != hash || !key.equals(e.key)))
522 <                    e = e.next;
523 <
524 <                V oldValue = null;
525 <                if (e != null) {
526 <                    V v = e.value;
527 <                    if (value == null || value.equals(v)) {
528 <                        oldValue = v;
529 <                        // All entries following removed node can stay
530 <                        // in list, but all preceding ones need to be
531 <                        // cloned.
532 <                        ++modCount;
533 <                        HashEntry<K,V> newFirst = e.next;
534 <                        for (HashEntry<K,V> p = first; p != e; p = p.next)
535 <                            newFirst = new HashEntry<K,V>(p.key, p.hash,
536 <                                                          newFirst, p.value);
537 <                        tab[index] = newFirst;
538 <                        count = c; // write-volatile
539 <                    }
540 <                }
541 <                return oldValue;
542 <            } finally {
543 <                unlock();
544 <            }
545 <        }
745 >    /**
746 >     * The array of bins. Lazily initialized upon first insertion.
747 >     * Size is always a power of two. Accessed directly by iterators.
748 >     */
749 >    transient volatile Node<K,V>[] table;
750  
751 <        void clear() {
752 <            if (count != 0) {
753 <                lock();
754 <                try {
551 <                    HashEntry<K,V>[] tab = table;
552 <                    for (int i = 0; i < tab.length ; i++)
553 <                        tab[i] = null;
554 <                    ++modCount;
555 <                    count = 0; // write-volatile
556 <                } finally {
557 <                    unlock();
558 <                }
559 <            }
560 <        }
561 <    }
751 >    /**
752 >     * The next table to use; non-null only while resizing.
753 >     */
754 >    private transient volatile Node<K,V>[] nextTable;
755  
756 +    /**
757 +     * Base counter value, used mainly when there is no contention,
758 +     * but also as a fallback during table initialization
759 +     * races. Updated via CAS.
760 +     */
761 +    private transient volatile long baseCount;
762  
763 +    /**
764 +     * Table initialization and resizing control.  When negative, the
765 +     * table is being initialized or resized: -1 for initialization,
766 +     * else -(1 + the number of active resizing threads).  Otherwise,
767 +     * when table is null, holds the initial table size to use upon
768 +     * creation, or 0 for default. After initialization, holds the
769 +     * next element count value upon which to resize the table.
770 +     */
771 +    private transient volatile int sizeCtl;
772  
773 <    /* ---------------- Public operations -------------- */
773 >    /**
774 >     * The next table index (plus one) to split while resizing.
775 >     */
776 >    private transient volatile int transferIndex;
777  
778      /**
779 <     * Creates a new, empty map with the specified initial
569 <     * capacity, load factor and concurrency level.
570 <     *
571 <     * @param initialCapacity the initial capacity. The implementation
572 <     * performs internal sizing to accommodate this many elements.
573 <     * @param loadFactor  the load factor threshold, used to control resizing.
574 <     * Resizing may be performed when the average number of elements per
575 <     * bin exceeds this threshold.
576 <     * @param concurrencyLevel the estimated number of concurrently
577 <     * updating threads. The implementation performs internal sizing
578 <     * to try to accommodate this many threads.
579 <     * @throws IllegalArgumentException if the initial capacity is
580 <     * negative or the load factor or concurrencyLevel are
581 <     * nonpositive.
779 >     * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
780       */
781 <    public ConcurrentHashMap(int initialCapacity,
584 <                             float loadFactor, int concurrencyLevel) {
585 <        if (!(loadFactor > 0) || initialCapacity < 0 || concurrencyLevel <= 0)
586 <            throw new IllegalArgumentException();
781 >    private transient volatile int cellsBusy;
782  
783 <        if (concurrencyLevel > MAX_SEGMENTS)
784 <            concurrencyLevel = MAX_SEGMENTS;
783 >    /**
784 >     * Table of counter cells. When non-null, size is a power of 2.
785 >     */
786 >    private transient volatile CounterCell[] counterCells;
787  
788 <        // Find power-of-two sizes best matching arguments
789 <        int sshift = 0;
790 <        int ssize = 1;
791 <        while (ssize < concurrencyLevel) {
595 <            ++sshift;
596 <            ssize <<= 1;
597 <        }
598 <        segmentShift = 32 - sshift;
599 <        segmentMask = ssize - 1;
600 <        this.segments = Segment.newArray(ssize);
601 <
602 <        if (initialCapacity > MAXIMUM_CAPACITY)
603 <            initialCapacity = MAXIMUM_CAPACITY;
604 <        int c = initialCapacity / ssize;
605 <        if (c * ssize < initialCapacity)
606 <            ++c;
607 <        int cap = 1;
608 <        while (cap < c)
609 <            cap <<= 1;
788 >    // views
789 >    private transient KeySetView<K,V> keySet;
790 >    private transient ValuesView<K,V> values;
791 >    private transient EntrySetView<K,V> entrySet;
792  
793 <        for (int i = 0; i < this.segments.length; ++i)
794 <            this.segments[i] = new Segment<K,V>(cap, loadFactor);
613 <    }
793 >
794 >    /* ---------------- Public operations -------------- */
795  
796      /**
797 <     * Creates a new, empty map with the specified initial capacity
617 <     * and load factor and with the default concurrencyLevel (16).
618 <     *
619 <     * @param initialCapacity The implementation performs internal
620 <     * sizing to accommodate this many elements.
621 <     * @param loadFactor  the load factor threshold, used to control resizing.
622 <     * Resizing may be performed when the average number of elements per
623 <     * bin exceeds this threshold.
624 <     * @throws IllegalArgumentException if the initial capacity of
625 <     * elements is negative or the load factor is nonpositive
626 <     *
627 <     * @since 1.6
797 >     * Creates a new, empty map with the default initial table size (16).
798       */
799 <    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
630 <        this(initialCapacity, loadFactor, DEFAULT_CONCURRENCY_LEVEL);
799 >    public ConcurrentHashMap() {
800      }
801  
802      /**
803 <     * Creates a new, empty map with the specified initial capacity,
804 <     * and with default load factor (0.75) and concurrencyLevel (16).
803 >     * Creates a new, empty map with an initial table size
804 >     * accommodating the specified number of elements without the need
805 >     * to dynamically resize.
806       *
807 <     * @param initialCapacity the initial capacity. The implementation
808 <     * performs internal sizing to accommodate this many elements.
807 >     * @param initialCapacity The implementation performs internal
808 >     * sizing to accommodate this many elements.
809       * @throws IllegalArgumentException if the initial capacity of
810 <     * elements is negative.
810 >     * elements is negative
811       */
812      public ConcurrentHashMap(int initialCapacity) {
813 <        this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
644 <    }
645 <
646 <    /**
647 <     * Creates a new, empty map with a default initial capacity (16),
648 <     * load factor (0.75) and concurrencyLevel (16).
649 <     */
650 <    public ConcurrentHashMap() {
651 <        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
813 >        this(initialCapacity, LOAD_FACTOR, 1);
814      }
815  
816      /**
817       * Creates a new map with the same mappings as the given map.
656     * The map is created with a capacity of 1.5 times the number
657     * of mappings in the given map or 16 (whichever is greater),
658     * and a default load factor (0.75) and concurrencyLevel (16).
818       *
819       * @param m the map
820       */
821      public ConcurrentHashMap(Map<? extends K, ? extends V> m) {
822 <        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
664 <                      DEFAULT_INITIAL_CAPACITY),
665 <             DEFAULT_LOAD_FACTOR, DEFAULT_CONCURRENCY_LEVEL);
822 >        this.sizeCtl = DEFAULT_CAPACITY;
823          putAll(m);
824      }
825  
826      /**
827 <     * Returns <tt>true</tt> if this map contains no key-value mappings.
827 >     * Creates a new, empty map with an initial table size based on
828 >     * the given number of elements ({@code initialCapacity}) and
829 >     * initial table density ({@code loadFactor}).
830       *
831 <     * @return <tt>true</tt> if this map contains no key-value mappings
831 >     * @param initialCapacity the initial capacity. The implementation
832 >     * performs internal sizing to accommodate this many elements,
833 >     * given the specified load factor.
834 >     * @param loadFactor the load factor (table density) for
835 >     * establishing the initial table size
836 >     * @throws IllegalArgumentException if the initial capacity of
837 >     * elements is negative or the load factor is nonpositive
838 >     *
839 >     * @since 1.6
840       */
841 <    public boolean isEmpty() {
842 <        final Segment<K,V>[] segments = this.segments;
676 <        /*
677 <         * We keep track of per-segment modCounts to avoid ABA
678 <         * problems in which an element in one segment was added and
679 <         * in another removed during traversal, in which case the
680 <         * table was never actually empty at any point. Note the
681 <         * similar use of modCounts in the size() and containsValue()
682 <         * methods, which are the only other methods also susceptible
683 <         * to ABA problems.
684 <         */
685 <        int[] mc = new int[segments.length];
686 <        int mcsum = 0;
687 <        for (int i = 0; i < segments.length; ++i) {
688 <            if (segments[i].count != 0)
689 <                return false;
690 <            else
691 <                mcsum += mc[i] = segments[i].modCount;
692 <        }
693 <        // If mcsum happens to be zero, then we know we got a snapshot
694 <        // before any modifications at all were made.  This is
695 <        // probably common enough to bother tracking.
696 <        if (mcsum != 0) {
697 <            for (int i = 0; i < segments.length; ++i) {
698 <                if (segments[i].count != 0 ||
699 <                    mc[i] != segments[i].modCount)
700 <                    return false;
701 <            }
702 <        }
703 <        return true;
841 >    public ConcurrentHashMap(int initialCapacity, float loadFactor) {
842 >        this(initialCapacity, loadFactor, 1);
843      }
844  
845      /**
846 <     * Returns the number of key-value mappings in this map.  If the
847 <     * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
848 <     * <tt>Integer.MAX_VALUE</tt>.
846 >     * Creates a new, empty map with an initial table size based on
847 >     * the given number of elements ({@code initialCapacity}), initial
848 >     * table density ({@code loadFactor}), and number of concurrently
849 >     * updating threads ({@code concurrencyLevel}).
850       *
851 <     * @return the number of key-value mappings in this map
851 >     * @param initialCapacity the initial capacity. The implementation
852 >     * performs internal sizing to accommodate this many elements,
853 >     * given the specified load factor.
854 >     * @param loadFactor the load factor (table density) for
855 >     * establishing the initial table size
856 >     * @param concurrencyLevel the estimated number of concurrently
857 >     * updating threads. The implementation may use this value as
858 >     * a sizing hint.
859 >     * @throws IllegalArgumentException if the initial capacity is
860 >     * negative or the load factor or concurrencyLevel are
861 >     * nonpositive
862 >     */
863 >    public ConcurrentHashMap(int initialCapacity,
864 >                             float loadFactor, int concurrencyLevel) {
865 >        if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
866 >            throw new IllegalArgumentException();
867 >        if (initialCapacity < concurrencyLevel)   // Use at least as many bins
868 >            initialCapacity = concurrencyLevel;   // as estimated threads
869 >        long size = (long)(1.0 + (long)initialCapacity / loadFactor);
870 >        int cap = (size >= (long)MAXIMUM_CAPACITY) ?
871 >            MAXIMUM_CAPACITY : tableSizeFor((int)size);
872 >        this.sizeCtl = cap;
873 >    }
874 >
875 >    // Original (since JDK1.2) Map methods
876 >
877 >    /**
878 >     * {@inheritDoc}
879       */
880      public int size() {
881 <        final Segment<K,V>[] segments = this.segments;
882 <        long sum = 0;
883 <        long check = 0;
884 <        int[] mc = new int[segments.length];
885 <        // Try a few times to get accurate count. On failure due to
886 <        // continuous async changes in table, resort to locking.
887 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
888 <            check = 0;
889 <            sum = 0;
890 <            int mcsum = 0;
891 <            for (int i = 0; i < segments.length; ++i) {
725 <                sum += segments[i].count;
726 <                mcsum += mc[i] = segments[i].modCount;
727 <            }
728 <            if (mcsum != 0) {
729 <                for (int i = 0; i < segments.length; ++i) {
730 <                    check += segments[i].count;
731 <                    if (mc[i] != segments[i].modCount) {
732 <                        check = -1; // force retry
733 <                        break;
734 <                    }
735 <                }
736 <            }
737 <            if (check == sum)
738 <                break;
739 <        }
740 <        if (check != sum) { // Resort to locking all segments
741 <            sum = 0;
742 <            for (int i = 0; i < segments.length; ++i)
743 <                segments[i].lock();
744 <            for (int i = 0; i < segments.length; ++i)
745 <                sum += segments[i].count;
746 <            for (int i = 0; i < segments.length; ++i)
747 <                segments[i].unlock();
748 <        }
749 <        if (sum > Integer.MAX_VALUE)
750 <            return Integer.MAX_VALUE;
751 <        else
752 <            return (int)sum;
881 >        long n = sumCount();
882 >        return ((n < 0L) ? 0 :
883 >                (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
884 >                (int)n);
885 >    }
886 >
887 >    /**
888 >     * {@inheritDoc}
889 >     */
890 >    public boolean isEmpty() {
891 >        return sumCount() <= 0L; // ignore transient negative values
892      }
893  
894      /**
# Line 764 | Line 903 | public class ConcurrentHashMap<K, V> ext
903       * @throws NullPointerException if the specified key is null
904       */
905      public V get(Object key) {
906 <        int hash = hash(key.hashCode());
907 <        return segmentFor(hash).get(key, hash);
906 >        Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
907 >        int h = spread(key.hashCode());
908 >        if ((tab = table) != null && (n = tab.length) > 0 &&
909 >            (e = tabAt(tab, (n - 1) & h)) != null) {
910 >            if ((eh = e.hash) == h) {
911 >                if ((ek = e.key) == key || (ek != null && key.equals(ek)))
912 >                    return e.val;
913 >            }
914 >            else if (eh < 0)
915 >                return (p = e.find(h, key)) != null ? p.val : null;
916 >            while ((e = e.next) != null) {
917 >                if (e.hash == h &&
918 >                    ((ek = e.key) == key || (ek != null && key.equals(ek))))
919 >                    return e.val;
920 >            }
921 >        }
922 >        return null;
923      }
924  
925      /**
926       * Tests if the specified object is a key in this table.
927       *
928 <     * @param  key   possible key
929 <     * @return <tt>true</tt> if and only if the specified object
928 >     * @param  key possible key
929 >     * @return {@code true} if and only if the specified object
930       *         is a key in this table, as determined by the
931 <     *         <tt>equals</tt> method; <tt>false</tt> otherwise.
931 >     *         {@code equals} method; {@code false} otherwise
932       * @throws NullPointerException if the specified key is null
933       */
934      public boolean containsKey(Object key) {
935 <        int hash = hash(key.hashCode());
782 <        return segmentFor(hash).containsKey(key, hash);
935 >        return get(key) != null;
936      }
937  
938      /**
939 <     * Returns <tt>true</tt> if this map maps one or more keys to the
940 <     * specified value. Note: This method requires a full internal
941 <     * traversal of the hash table, and so is much slower than
789 <     * method <tt>containsKey</tt>.
939 >     * Returns {@code true} if this map maps one or more keys to the
940 >     * specified value. Note: This method may require a full traversal
941 >     * of the map, and is much slower than method {@code containsKey}.
942       *
943       * @param value value whose presence in this map is to be tested
944 <     * @return <tt>true</tt> if this map maps one or more keys to the
944 >     * @return {@code true} if this map maps one or more keys to the
945       *         specified value
946       * @throws NullPointerException if the specified value is null
947       */
948      public boolean containsValue(Object value) {
949          if (value == null)
950              throw new NullPointerException();
951 <
952 <        // See explanation of modCount use above
953 <
954 <        final Segment<K,V>[] segments = this.segments;
955 <        int[] mc = new int[segments.length];
956 <
805 <        // Try a few times without locking
806 <        for (int k = 0; k < RETRIES_BEFORE_LOCK; ++k) {
807 <            int sum = 0;
808 <            int mcsum = 0;
809 <            for (int i = 0; i < segments.length; ++i) {
810 <                int c = segments[i].count;
811 <                mcsum += mc[i] = segments[i].modCount;
812 <                if (segments[i].containsValue(value))
951 >        Node<K,V>[] t;
952 >        if ((t = table) != null) {
953 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
954 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
955 >                V v;
956 >                if ((v = p.val) == value || (v != null && value.equals(v)))
957                      return true;
958              }
815            boolean cleanSweep = true;
816            if (mcsum != 0) {
817                for (int i = 0; i < segments.length; ++i) {
818                    int c = segments[i].count;
819                    if (mc[i] != segments[i].modCount) {
820                        cleanSweep = false;
821                        break;
822                    }
823                }
824            }
825            if (cleanSweep)
826                return false;
959          }
960 <        // Resort to locking all segments
829 <        for (int i = 0; i < segments.length; ++i)
830 <            segments[i].lock();
831 <        boolean found = false;
832 <        try {
833 <            for (int i = 0; i < segments.length; ++i) {
834 <                if (segments[i].containsValue(value)) {
835 <                    found = true;
836 <                    break;
837 <                }
838 <            }
839 <        } finally {
840 <            for (int i = 0; i < segments.length; ++i)
841 <                segments[i].unlock();
842 <        }
843 <        return found;
844 <    }
845 <
846 <    /**
847 <     * Legacy method testing if some key maps into the specified value
848 <     * in this table.  This method is identical in functionality to
849 <     * {@link #containsValue}, and exists solely to ensure
850 <     * full compatibility with class {@link java.util.Hashtable},
851 <     * which supported this method prior to introduction of the
852 <     * Java Collections framework.
853 <
854 <     * @param  value a value to search for
855 <     * @return <tt>true</tt> if and only if some key maps to the
856 <     *         <tt>value</tt> argument in this table as
857 <     *         determined by the <tt>equals</tt> method;
858 <     *         <tt>false</tt> otherwise
859 <     * @throws NullPointerException if the specified value is null
860 <     */
861 <    public boolean contains(Object value) {
862 <        return containsValue(value);
960 >        return false;
961      }
962  
963      /**
964       * Maps the specified key to the specified value in this table.
965       * Neither the key nor the value can be null.
966       *
967 <     * <p> The value can be retrieved by calling the <tt>get</tt> method
967 >     * <p>The value can be retrieved by calling the {@code get} method
968       * with a key that is equal to the original key.
969       *
970       * @param key key with which the specified value is to be associated
971       * @param value value to be associated with the specified key
972 <     * @return the previous value associated with <tt>key</tt>, or
973 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
972 >     * @return the previous value associated with {@code key}, or
973 >     *         {@code null} if there was no mapping for {@code key}
974       * @throws NullPointerException if the specified key or value is null
975       */
976      public V put(K key, V value) {
977 <        if (value == null)
880 <            throw new NullPointerException();
881 <        int hash = hash(key.hashCode());
882 <        return segmentFor(hash).put(key, hash, value, false);
977 >        return putVal(key, value, false);
978      }
979  
980 <    /**
981 <     * {@inheritDoc}
982 <     *
983 <     * @return the previous value associated with the specified key,
984 <     *         or <tt>null</tt> if there was no mapping for the key
985 <     * @throws NullPointerException if the specified key or value is null
986 <     */
987 <    public V putIfAbsent(K key, V value) {
988 <        if (value == null)
989 <            throw new NullPointerException();
990 <        int hash = hash(key.hashCode());
991 <        return segmentFor(hash).put(key, hash, value, true);
980 >    /** Implementation for put and putIfAbsent */
981 >    final V putVal(K key, V value, boolean onlyIfAbsent) {
982 >        if (key == null || value == null) throw new NullPointerException();
983 >        int hash = spread(key.hashCode());
984 >        int binCount = 0;
985 >        for (Node<K,V>[] tab = table;;) {
986 >            Node<K,V> f; int n, i, fh; K fk; V fv;
987 >            if (tab == null || (n = tab.length) == 0)
988 >                tab = initTable();
989 >            else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
990 >                if (casTabAt(tab, i, null, new Node<K,V>(hash, key, value)))
991 >                    break;                   // no lock when adding to empty bin
992 >            }
993 >            else if ((fh = f.hash) == MOVED)
994 >                tab = helpTransfer(tab, f);
995 >            else if (onlyIfAbsent // check first node without acquiring lock
996 >                     && fh == hash
997 >                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
998 >                     && (fv = f.val) != null)
999 >                return fv;
1000 >            else {
1001 >                V oldVal = null;
1002 >                synchronized (f) {
1003 >                    if (tabAt(tab, i) == f) {
1004 >                        if (fh >= 0) {
1005 >                            binCount = 1;
1006 >                            for (Node<K,V> e = f;; ++binCount) {
1007 >                                K ek;
1008 >                                if (e.hash == hash &&
1009 >                                    ((ek = e.key) == key ||
1010 >                                     (ek != null && key.equals(ek)))) {
1011 >                                    oldVal = e.val;
1012 >                                    if (!onlyIfAbsent)
1013 >                                        e.val = value;
1014 >                                    break;
1015 >                                }
1016 >                                Node<K,V> pred = e;
1017 >                                if ((e = e.next) == null) {
1018 >                                    pred.next = new Node<K,V>(hash, key, value);
1019 >                                    break;
1020 >                                }
1021 >                            }
1022 >                        }
1023 >                        else if (f instanceof TreeBin) {
1024 >                            Node<K,V> p;
1025 >                            binCount = 2;
1026 >                            if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1027 >                                                           value)) != null) {
1028 >                                oldVal = p.val;
1029 >                                if (!onlyIfAbsent)
1030 >                                    p.val = value;
1031 >                            }
1032 >                        }
1033 >                        else if (f instanceof ReservationNode)
1034 >                            throw new IllegalStateException("Recursive update");
1035 >                    }
1036 >                }
1037 >                if (binCount != 0) {
1038 >                    if (binCount >= TREEIFY_THRESHOLD)
1039 >                        treeifyBin(tab, i);
1040 >                    if (oldVal != null)
1041 >                        return oldVal;
1042 >                    break;
1043 >                }
1044 >            }
1045 >        }
1046 >        addCount(1L, binCount);
1047 >        return null;
1048      }
1049  
1050      /**
# Line 904 | Line 1055 | public class ConcurrentHashMap<K, V> ext
1055       * @param m mappings to be stored in this map
1056       */
1057      public void putAll(Map<? extends K, ? extends V> m) {
1058 +        tryPresize(m.size());
1059          for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1060 <            put(e.getKey(), e.getValue());
1060 >            putVal(e.getKey(), e.getValue(), false);
1061      }
1062  
1063      /**
# Line 913 | Line 1065 | public class ConcurrentHashMap<K, V> ext
1065       * This method does nothing if the key is not in the map.
1066       *
1067       * @param  key the key that needs to be removed
1068 <     * @return the previous value associated with <tt>key</tt>, or
1069 <     *         <tt>null</tt> if there was no mapping for <tt>key</tt>
1068 >     * @return the previous value associated with {@code key}, or
1069 >     *         {@code null} if there was no mapping for {@code key}
1070       * @throws NullPointerException if the specified key is null
1071       */
1072      public V remove(Object key) {
1073 <        int hash = hash(key.hashCode());
1074 <        return segmentFor(hash).remove(key, hash, null);
1073 >        return replaceNode(key, null, null);
1074 >    }
1075 >
1076 >    /**
1077 >     * Implementation for the four public remove/replace methods:
1078 >     * Replaces node value with v, conditional upon match of cv if
1079 >     * non-null.  If resulting value is null, delete.
1080 >     */
1081 >    final V replaceNode(Object key, V value, Object cv) {
1082 >        int hash = spread(key.hashCode());
1083 >        for (Node<K,V>[] tab = table;;) {
1084 >            Node<K,V> f; int n, i, fh;
1085 >            if (tab == null || (n = tab.length) == 0 ||
1086 >                (f = tabAt(tab, i = (n - 1) & hash)) == null)
1087 >                break;
1088 >            else if ((fh = f.hash) == MOVED)
1089 >                tab = helpTransfer(tab, f);
1090 >            else {
1091 >                V oldVal = null;
1092 >                boolean validated = false;
1093 >                synchronized (f) {
1094 >                    if (tabAt(tab, i) == f) {
1095 >                        if (fh >= 0) {
1096 >                            validated = true;
1097 >                            for (Node<K,V> e = f, pred = null;;) {
1098 >                                K ek;
1099 >                                if (e.hash == hash &&
1100 >                                    ((ek = e.key) == key ||
1101 >                                     (ek != null && key.equals(ek)))) {
1102 >                                    V ev = e.val;
1103 >                                    if (cv == null || cv == ev ||
1104 >                                        (ev != null && cv.equals(ev))) {
1105 >                                        oldVal = ev;
1106 >                                        if (value != null)
1107 >                                            e.val = value;
1108 >                                        else if (pred != null)
1109 >                                            pred.next = e.next;
1110 >                                        else
1111 >                                            setTabAt(tab, i, e.next);
1112 >                                    }
1113 >                                    break;
1114 >                                }
1115 >                                pred = e;
1116 >                                if ((e = e.next) == null)
1117 >                                    break;
1118 >                            }
1119 >                        }
1120 >                        else if (f instanceof TreeBin) {
1121 >                            validated = true;
1122 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1123 >                            TreeNode<K,V> r, p;
1124 >                            if ((r = t.root) != null &&
1125 >                                (p = r.findTreeNode(hash, key, null)) != null) {
1126 >                                V pv = p.val;
1127 >                                if (cv == null || cv == pv ||
1128 >                                    (pv != null && cv.equals(pv))) {
1129 >                                    oldVal = pv;
1130 >                                    if (value != null)
1131 >                                        p.val = value;
1132 >                                    else if (t.removeTreeNode(p))
1133 >                                        setTabAt(tab, i, untreeify(t.first));
1134 >                                }
1135 >                            }
1136 >                        }
1137 >                        else if (f instanceof ReservationNode)
1138 >                            throw new IllegalStateException("Recursive update");
1139 >                    }
1140 >                }
1141 >                if (validated) {
1142 >                    if (oldVal != null) {
1143 >                        if (value == null)
1144 >                            addCount(-1L, -1);
1145 >                        return oldVal;
1146 >                    }
1147 >                    break;
1148 >                }
1149 >            }
1150 >        }
1151 >        return null;
1152 >    }
1153 >
1154 >    /**
1155 >     * Removes all of the mappings from this map.
1156 >     */
1157 >    public void clear() {
1158 >        long delta = 0L; // negative number of deletions
1159 >        int i = 0;
1160 >        Node<K,V>[] tab = table;
1161 >        while (tab != null && i < tab.length) {
1162 >            int fh;
1163 >            Node<K,V> f = tabAt(tab, i);
1164 >            if (f == null)
1165 >                ++i;
1166 >            else if ((fh = f.hash) == MOVED) {
1167 >                tab = helpTransfer(tab, f);
1168 >                i = 0; // restart
1169 >            }
1170 >            else {
1171 >                synchronized (f) {
1172 >                    if (tabAt(tab, i) == f) {
1173 >                        Node<K,V> p = (fh >= 0 ? f :
1174 >                                       (f instanceof TreeBin) ?
1175 >                                       ((TreeBin<K,V>)f).first : null);
1176 >                        while (p != null) {
1177 >                            --delta;
1178 >                            p = p.next;
1179 >                        }
1180 >                        setTabAt(tab, i++, null);
1181 >                    }
1182 >                }
1183 >            }
1184 >        }
1185 >        if (delta != 0L)
1186 >            addCount(delta, -1);
1187 >    }
1188 >
1189 >    /**
1190 >     * Returns a {@link Set} view of the keys contained in this map.
1191 >     * The set is backed by the map, so changes to the map are
1192 >     * reflected in the set, and vice-versa. The set supports element
1193 >     * removal, which removes the corresponding mapping from this map,
1194 >     * via the {@code Iterator.remove}, {@code Set.remove},
1195 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
1196 >     * operations.  It does not support the {@code add} or
1197 >     * {@code addAll} operations.
1198 >     *
1199 >     * <p>The view's iterators and spliterators are
1200 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1201 >     *
1202 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
1203 >     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
1204 >     *
1205 >     * @return the set view
1206 >     */
1207 >    public KeySetView<K,V> keySet() {
1208 >        KeySetView<K,V> ks;
1209 >        if ((ks = keySet) != null) return ks;
1210 >        return keySet = new KeySetView<K,V>(this, null);
1211 >    }
1212 >
1213 >    /**
1214 >     * Returns a {@link Collection} view of the values contained in this map.
1215 >     * The collection is backed by the map, so changes to the map are
1216 >     * reflected in the collection, and vice-versa.  The collection
1217 >     * supports element removal, which removes the corresponding
1218 >     * mapping from this map, via the {@code Iterator.remove},
1219 >     * {@code Collection.remove}, {@code removeAll},
1220 >     * {@code retainAll}, and {@code clear} operations.  It does not
1221 >     * support the {@code add} or {@code addAll} operations.
1222 >     *
1223 >     * <p>The view's iterators and spliterators are
1224 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1225 >     *
1226 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}
1227 >     * and {@link Spliterator#NONNULL}.
1228 >     *
1229 >     * @return the collection view
1230 >     */
1231 >    public Collection<V> values() {
1232 >        ValuesView<K,V> vs;
1233 >        if ((vs = values) != null) return vs;
1234 >        return values = new ValuesView<K,V>(this);
1235 >    }
1236 >
1237 >    /**
1238 >     * Returns a {@link Set} view of the mappings contained in this map.
1239 >     * The set is backed by the map, so changes to the map are
1240 >     * reflected in the set, and vice-versa.  The set supports element
1241 >     * removal, which removes the corresponding mapping from the map,
1242 >     * via the {@code Iterator.remove}, {@code Set.remove},
1243 >     * {@code removeAll}, {@code retainAll}, and {@code clear}
1244 >     * operations.
1245 >     *
1246 >     * <p>The view's iterators and spliterators are
1247 >     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
1248 >     *
1249 >     * <p>The view's {@code spliterator} reports {@link Spliterator#CONCURRENT},
1250 >     * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}.
1251 >     *
1252 >     * @return the set view
1253 >     */
1254 >    public Set<Map.Entry<K,V>> entrySet() {
1255 >        EntrySetView<K,V> es;
1256 >        if ((es = entrySet) != null) return es;
1257 >        return entrySet = new EntrySetView<K,V>(this);
1258 >    }
1259 >
1260 >    /**
1261 >     * Returns the hash code value for this {@link Map}, i.e.,
1262 >     * the sum of, for each key-value pair in the map,
1263 >     * {@code key.hashCode() ^ value.hashCode()}.
1264 >     *
1265 >     * @return the hash code value for this map
1266 >     */
1267 >    public int hashCode() {
1268 >        int h = 0;
1269 >        Node<K,V>[] t;
1270 >        if ((t = table) != null) {
1271 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1272 >            for (Node<K,V> p; (p = it.advance()) != null; )
1273 >                h += p.key.hashCode() ^ p.val.hashCode();
1274 >        }
1275 >        return h;
1276 >    }
1277 >
1278 >    /**
1279 >     * Returns a string representation of this map.  The string
1280 >     * representation consists of a list of key-value mappings (in no
1281 >     * particular order) enclosed in braces ("{@code {}}").  Adjacent
1282 >     * mappings are separated by the characters {@code ", "} (comma
1283 >     * and space).  Each key-value mapping is rendered as the key
1284 >     * followed by an equals sign ("{@code =}") followed by the
1285 >     * associated value.
1286 >     *
1287 >     * @return a string representation of this map
1288 >     */
1289 >    public String toString() {
1290 >        Node<K,V>[] t;
1291 >        int f = (t = table) == null ? 0 : t.length;
1292 >        Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1293 >        StringBuilder sb = new StringBuilder();
1294 >        sb.append('{');
1295 >        Node<K,V> p;
1296 >        if ((p = it.advance()) != null) {
1297 >            for (;;) {
1298 >                K k = p.key;
1299 >                V v = p.val;
1300 >                sb.append(k == this ? "(this Map)" : k);
1301 >                sb.append('=');
1302 >                sb.append(v == this ? "(this Map)" : v);
1303 >                if ((p = it.advance()) == null)
1304 >                    break;
1305 >                sb.append(',').append(' ');
1306 >            }
1307 >        }
1308 >        return sb.append('}').toString();
1309 >    }
1310 >
1311 >    /**
1312 >     * Compares the specified object with this map for equality.
1313 >     * Returns {@code true} if the given object is a map with the same
1314 >     * mappings as this map.  This operation may return misleading
1315 >     * results if either map is concurrently modified during execution
1316 >     * of this method.
1317 >     *
1318 >     * @param o object to be compared for equality with this map
1319 >     * @return {@code true} if the specified object is equal to this map
1320 >     */
1321 >    public boolean equals(Object o) {
1322 >        if (o != this) {
1323 >            if (!(o instanceof Map))
1324 >                return false;
1325 >            Map<?,?> m = (Map<?,?>) o;
1326 >            Node<K,V>[] t;
1327 >            int f = (t = table) == null ? 0 : t.length;
1328 >            Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1329 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1330 >                V val = p.val;
1331 >                Object v = m.get(p.key);
1332 >                if (v == null || (v != val && !v.equals(val)))
1333 >                    return false;
1334 >            }
1335 >            for (Map.Entry<?,?> e : m.entrySet()) {
1336 >                Object mk, mv, v;
1337 >                if ((mk = e.getKey()) == null ||
1338 >                    (mv = e.getValue()) == null ||
1339 >                    (v = get(mk)) == null ||
1340 >                    (mv != v && !mv.equals(v)))
1341 >                    return false;
1342 >            }
1343 >        }
1344 >        return true;
1345 >    }
1346 >
1347 >    /**
1348 >     * Stripped-down version of helper class used in previous version,
1349 >     * declared for the sake of serialization compatibility.
1350 >     */
1351 >    static class Segment<K,V> extends ReentrantLock implements Serializable {
1352 >        private static final long serialVersionUID = 2249069246763182397L;
1353 >        final float loadFactor;
1354 >        Segment(float lf) { this.loadFactor = lf; }
1355 >    }
1356 >
1357 >    /**
1358 >     * Saves this map to a stream (that is, serializes it).
1359 >     *
1360 >     * @param s the stream
1361 >     * @throws java.io.IOException if an I/O error occurs
1362 >     * @serialData
1363 >     * the serialized fields, followed by the key (Object) and value
1364 >     * (Object) for each key-value mapping, followed by a null pair.
1365 >     * The key-value mappings are emitted in no particular order.
1366 >     */
1367 >    private void writeObject(java.io.ObjectOutputStream s)
1368 >        throws java.io.IOException {
1369 >        // For serialization compatibility
1370 >        // Emulate segment calculation from previous version of this class
1371 >        int sshift = 0;
1372 >        int ssize = 1;
1373 >        while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1374 >            ++sshift;
1375 >            ssize <<= 1;
1376 >        }
1377 >        int segmentShift = 32 - sshift;
1378 >        int segmentMask = ssize - 1;
1379 >        @SuppressWarnings("unchecked")
1380 >        Segment<K,V>[] segments = (Segment<K,V>[])
1381 >            new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1382 >        for (int i = 0; i < segments.length; ++i)
1383 >            segments[i] = new Segment<K,V>(LOAD_FACTOR);
1384 >        java.io.ObjectOutputStream.PutField streamFields = s.putFields();
1385 >        streamFields.put("segments", segments);
1386 >        streamFields.put("segmentShift", segmentShift);
1387 >        streamFields.put("segmentMask", segmentMask);
1388 >        s.writeFields();
1389 >
1390 >        Node<K,V>[] t;
1391 >        if ((t = table) != null) {
1392 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1393 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1394 >                s.writeObject(p.key);
1395 >                s.writeObject(p.val);
1396 >            }
1397 >        }
1398 >        s.writeObject(null);
1399 >        s.writeObject(null);
1400 >    }
1401 >
1402 >    /**
1403 >     * Reconstitutes this map from a stream (that is, deserializes it).
1404 >     * @param s the stream
1405 >     * @throws ClassNotFoundException if the class of a serialized object
1406 >     *         could not be found
1407 >     * @throws java.io.IOException if an I/O error occurs
1408 >     */
1409 >    private void readObject(java.io.ObjectInputStream s)
1410 >        throws java.io.IOException, ClassNotFoundException {
1411 >        /*
1412 >         * To improve performance in typical cases, we create nodes
1413 >         * while reading, then place in table once size is known.
1414 >         * However, we must also validate uniqueness and deal with
1415 >         * overpopulated bins while doing so, which requires
1416 >         * specialized versions of putVal mechanics.
1417 >         */
1418 >        sizeCtl = -1; // force exclusion for table construction
1419 >        s.defaultReadObject();
1420 >        long size = 0L;
1421 >        Node<K,V> p = null;
1422 >        for (;;) {
1423 >            @SuppressWarnings("unchecked")
1424 >            K k = (K) s.readObject();
1425 >            @SuppressWarnings("unchecked")
1426 >            V v = (V) s.readObject();
1427 >            if (k != null && v != null) {
1428 >                p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1429 >                ++size;
1430 >            }
1431 >            else
1432 >                break;
1433 >        }
1434 >        if (size == 0L)
1435 >            sizeCtl = 0;
1436 >        else {
1437 >            long ts = (long)(1.0 + size / LOAD_FACTOR);
1438 >            int n = (ts >= (long)MAXIMUM_CAPACITY) ?
1439 >                MAXIMUM_CAPACITY : tableSizeFor((int)ts);
1440 >            @SuppressWarnings("unchecked")
1441 >            Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1442 >            int mask = n - 1;
1443 >            long added = 0L;
1444 >            while (p != null) {
1445 >                boolean insertAtFront;
1446 >                Node<K,V> next = p.next, first;
1447 >                int h = p.hash, j = h & mask;
1448 >                if ((first = tabAt(tab, j)) == null)
1449 >                    insertAtFront = true;
1450 >                else {
1451 >                    K k = p.key;
1452 >                    if (first.hash < 0) {
1453 >                        TreeBin<K,V> t = (TreeBin<K,V>)first;
1454 >                        if (t.putTreeVal(h, k, p.val) == null)
1455 >                            ++added;
1456 >                        insertAtFront = false;
1457 >                    }
1458 >                    else {
1459 >                        int binCount = 0;
1460 >                        insertAtFront = true;
1461 >                        Node<K,V> q; K qk;
1462 >                        for (q = first; q != null; q = q.next) {
1463 >                            if (q.hash == h &&
1464 >                                ((qk = q.key) == k ||
1465 >                                 (qk != null && k.equals(qk)))) {
1466 >                                insertAtFront = false;
1467 >                                break;
1468 >                            }
1469 >                            ++binCount;
1470 >                        }
1471 >                        if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1472 >                            insertAtFront = false;
1473 >                            ++added;
1474 >                            p.next = first;
1475 >                            TreeNode<K,V> hd = null, tl = null;
1476 >                            for (q = p; q != null; q = q.next) {
1477 >                                TreeNode<K,V> t = new TreeNode<K,V>
1478 >                                    (q.hash, q.key, q.val, null, null);
1479 >                                if ((t.prev = tl) == null)
1480 >                                    hd = t;
1481 >                                else
1482 >                                    tl.next = t;
1483 >                                tl = t;
1484 >                            }
1485 >                            setTabAt(tab, j, new TreeBin<K,V>(hd));
1486 >                        }
1487 >                    }
1488 >                }
1489 >                if (insertAtFront) {
1490 >                    ++added;
1491 >                    p.next = first;
1492 >                    setTabAt(tab, j, p);
1493 >                }
1494 >                p = next;
1495 >            }
1496 >            table = tab;
1497 >            sizeCtl = n - (n >>> 2);
1498 >            baseCount = added;
1499 >        }
1500 >    }
1501 >
1502 >    // ConcurrentMap methods
1503 >
1504 >    /**
1505 >     * {@inheritDoc}
1506 >     *
1507 >     * @return the previous value associated with the specified key,
1508 >     *         or {@code null} if there was no mapping for the key
1509 >     * @throws NullPointerException if the specified key or value is null
1510 >     */
1511 >    public V putIfAbsent(K key, V value) {
1512 >        return putVal(key, value, true);
1513      }
1514  
1515      /**
# Line 928 | Line 1518 | public class ConcurrentHashMap<K, V> ext
1518       * @throws NullPointerException if the specified key is null
1519       */
1520      public boolean remove(Object key, Object value) {
1521 <        int hash = hash(key.hashCode());
1522 <        if (value == null)
1523 <            return false;
934 <        return segmentFor(hash).remove(key, hash, value) != null;
1521 >        if (key == null)
1522 >            throw new NullPointerException();
1523 >        return value != null && replaceNode(key, null, value) != null;
1524      }
1525  
1526      /**
# Line 940 | Line 1529 | public class ConcurrentHashMap<K, V> ext
1529       * @throws NullPointerException if any of the arguments are null
1530       */
1531      public boolean replace(K key, V oldValue, V newValue) {
1532 <        if (oldValue == null || newValue == null)
1532 >        if (key == null || oldValue == null || newValue == null)
1533              throw new NullPointerException();
1534 <        int hash = hash(key.hashCode());
946 <        return segmentFor(hash).replace(key, hash, oldValue, newValue);
1534 >        return replaceNode(key, newValue, oldValue) != null;
1535      }
1536  
1537      /**
1538       * {@inheritDoc}
1539       *
1540       * @return the previous value associated with the specified key,
1541 <     *         or <tt>null</tt> if there was no mapping for the key
1541 >     *         or {@code null} if there was no mapping for the key
1542       * @throws NullPointerException if the specified key or value is null
1543       */
1544      public V replace(K key, V value) {
1545 <        if (value == null)
1545 >        if (key == null || value == null)
1546              throw new NullPointerException();
1547 <        int hash = hash(key.hashCode());
960 <        return segmentFor(hash).replace(key, hash, value);
1547 >        return replaceNode(key, value, null);
1548      }
1549  
1550 +    // Overrides of JDK8+ Map extension method defaults
1551 +
1552      /**
1553 <     * Removes all of the mappings from this map.
1553 >     * Returns the value to which the specified key is mapped, or the
1554 >     * given default value if this map contains no mapping for the
1555 >     * key.
1556 >     *
1557 >     * @param key the key whose associated value is to be returned
1558 >     * @param defaultValue the value to return if this map contains
1559 >     * no mapping for the given key
1560 >     * @return the mapping for the key, if present; else the default value
1561 >     * @throws NullPointerException if the specified key is null
1562       */
1563 <    public void clear() {
1564 <        for (int i = 0; i < segments.length; ++i)
1565 <            segments[i].clear();
1563 >    public V getOrDefault(Object key, V defaultValue) {
1564 >        V v;
1565 >        return (v = get(key)) == null ? defaultValue : v;
1566 >    }
1567 >
1568 >    public void forEach(BiConsumer<? super K, ? super V> action) {
1569 >        if (action == null) throw new NullPointerException();
1570 >        Node<K,V>[] t;
1571 >        if ((t = table) != null) {
1572 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1573 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1574 >                action.accept(p.key, p.val);
1575 >            }
1576 >        }
1577 >    }
1578 >
1579 >    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
1580 >        if (function == null) throw new NullPointerException();
1581 >        Node<K,V>[] t;
1582 >        if ((t = table) != null) {
1583 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1584 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1585 >                V oldValue = p.val;
1586 >                for (K key = p.key;;) {
1587 >                    V newValue = function.apply(key, oldValue);
1588 >                    if (newValue == null)
1589 >                        throw new NullPointerException();
1590 >                    if (replaceNode(key, newValue, oldValue) != null ||
1591 >                        (oldValue = get(key)) == null)
1592 >                        break;
1593 >                }
1594 >            }
1595 >        }
1596      }
1597  
1598      /**
1599 <     * Returns a {@link Set} view of the keys contained in this map.
1600 <     * The set is backed by the map, so changes to the map are
1601 <     * reflected in the set, and vice-versa.  The set supports element
1602 <     * removal, which removes the corresponding mapping from this map,
1603 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1604 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1605 <     * operations.  It does not support the <tt>add</tt> or
1606 <     * <tt>addAll</tt> operations.
1607 <     *
1608 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1609 <     * that will never throw {@link ConcurrentModificationException},
1610 <     * and guarantees to traverse elements as they existed upon
1611 <     * construction of the iterator, and may (but is not guaranteed to)
1612 <     * reflect any modifications subsequent to construction.
1613 <     */
1614 <    public Set<K> keySet() {
1615 <        Set<K> ks = keySet;
989 <        return (ks != null) ? ks : (keySet = new KeySet());
1599 >     * Helper method for EntrySetView.removeIf.
1600 >     */
1601 >    boolean removeEntryIf(Predicate<? super Entry<K,V>> function) {
1602 >        if (function == null) throw new NullPointerException();
1603 >        Node<K,V>[] t;
1604 >        boolean removed = false;
1605 >        if ((t = table) != null) {
1606 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1607 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1608 >                K k = p.key;
1609 >                V v = p.val;
1610 >                Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v);
1611 >                if (function.test(e) && replaceNode(k, null, v) != null)
1612 >                    removed = true;
1613 >            }
1614 >        }
1615 >        return removed;
1616      }
1617  
1618      /**
1619 <     * Returns a {@link Collection} view of the values contained in this map.
994 <     * The collection is backed by the map, so changes to the map are
995 <     * reflected in the collection, and vice-versa.  The collection
996 <     * supports element removal, which removes the corresponding
997 <     * mapping from this map, via the <tt>Iterator.remove</tt>,
998 <     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
999 <     * <tt>retainAll</tt>, and <tt>clear</tt> operations.  It does not
1000 <     * support the <tt>add</tt> or <tt>addAll</tt> operations.
1001 <     *
1002 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1003 <     * that will never throw {@link ConcurrentModificationException},
1004 <     * and guarantees to traverse elements as they existed upon
1005 <     * construction of the iterator, and may (but is not guaranteed to)
1006 <     * reflect any modifications subsequent to construction.
1619 >     * Helper method for ValuesView.removeIf.
1620       */
1621 <    public Collection<V> values() {
1622 <        Collection<V> vs = values;
1623 <        return (vs != null) ? vs : (values = new Values());
1621 >    boolean removeValueIf(Predicate<? super V> function) {
1622 >        if (function == null) throw new NullPointerException();
1623 >        Node<K,V>[] t;
1624 >        boolean removed = false;
1625 >        if ((t = table) != null) {
1626 >            Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1627 >            for (Node<K,V> p; (p = it.advance()) != null; ) {
1628 >                K k = p.key;
1629 >                V v = p.val;
1630 >                if (function.test(v) && replaceNode(k, null, v) != null)
1631 >                    removed = true;
1632 >            }
1633 >        }
1634 >        return removed;
1635      }
1636  
1637      /**
1638 <     * Returns a {@link Set} view of the mappings contained in this map.
1639 <     * The set is backed by the map, so changes to the map are
1640 <     * reflected in the set, and vice-versa.  The set supports element
1641 <     * removal, which removes the corresponding mapping from the map,
1642 <     * via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
1643 <     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
1644 <     * operations.  It does not support the <tt>add</tt> or
1645 <     * <tt>addAll</tt> operations.
1646 <     *
1647 <     * <p>The view's <tt>iterator</tt> is a "weakly consistent" iterator
1648 <     * that will never throw {@link ConcurrentModificationException},
1649 <     * and guarantees to traverse elements as they existed upon
1650 <     * construction of the iterator, and may (but is not guaranteed to)
1651 <     * reflect any modifications subsequent to construction.
1638 >     * If the specified key is not already associated with a value,
1639 >     * attempts to compute its value using the given mapping function
1640 >     * and enters it into this map unless {@code null}.  The entire
1641 >     * method invocation is performed atomically, so the function is
1642 >     * applied at most once per key.  Some attempted update operations
1643 >     * on this map by other threads may be blocked while computation
1644 >     * is in progress, so the computation should be short and simple,
1645 >     * and must not attempt to update any other mappings of this map.
1646 >     *
1647 >     * @param key key with which the specified value is to be associated
1648 >     * @param mappingFunction the function to compute a value
1649 >     * @return the current (existing or computed) value associated with
1650 >     *         the specified key, or null if the computed value is null
1651 >     * @throws NullPointerException if the specified key or mappingFunction
1652 >     *         is null
1653 >     * @throws IllegalStateException if the computation detectably
1654 >     *         attempts a recursive update to this map that would
1655 >     *         otherwise never complete
1656 >     * @throws RuntimeException or Error if the mappingFunction does so,
1657 >     *         in which case the mapping is left unestablished
1658       */
1659 <    public Set<Map.Entry<K,V>> entrySet() {
1660 <        Set<Map.Entry<K,V>> es = entrySet;
1661 <        return (es != null) ? es : (entrySet = new EntrySet());
1659 >    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
1660 >        if (key == null || mappingFunction == null)
1661 >            throw new NullPointerException();
1662 >        int h = spread(key.hashCode());
1663 >        V val = null;
1664 >        int binCount = 0;
1665 >        for (Node<K,V>[] tab = table;;) {
1666 >            Node<K,V> f; int n, i, fh; K fk; V fv;
1667 >            if (tab == null || (n = tab.length) == 0)
1668 >                tab = initTable();
1669 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1670 >                Node<K,V> r = new ReservationNode<K,V>();
1671 >                synchronized (r) {
1672 >                    if (casTabAt(tab, i, null, r)) {
1673 >                        binCount = 1;
1674 >                        Node<K,V> node = null;
1675 >                        try {
1676 >                            if ((val = mappingFunction.apply(key)) != null)
1677 >                                node = new Node<K,V>(h, key, val);
1678 >                        } finally {
1679 >                            setTabAt(tab, i, node);
1680 >                        }
1681 >                    }
1682 >                }
1683 >                if (binCount != 0)
1684 >                    break;
1685 >            }
1686 >            else if ((fh = f.hash) == MOVED)
1687 >                tab = helpTransfer(tab, f);
1688 >            else if (fh == h    // check first node without acquiring lock
1689 >                     && ((fk = f.key) == key || (fk != null && key.equals(fk)))
1690 >                     && (fv = f.val) != null)
1691 >                return fv;
1692 >            else {
1693 >                boolean added = false;
1694 >                synchronized (f) {
1695 >                    if (tabAt(tab, i) == f) {
1696 >                        if (fh >= 0) {
1697 >                            binCount = 1;
1698 >                            for (Node<K,V> e = f;; ++binCount) {
1699 >                                K ek;
1700 >                                if (e.hash == h &&
1701 >                                    ((ek = e.key) == key ||
1702 >                                     (ek != null && key.equals(ek)))) {
1703 >                                    val = e.val;
1704 >                                    break;
1705 >                                }
1706 >                                Node<K,V> pred = e;
1707 >                                if ((e = e.next) == null) {
1708 >                                    if ((val = mappingFunction.apply(key)) != null) {
1709 >                                        if (pred.next != null)
1710 >                                            throw new IllegalStateException("Recursive update");
1711 >                                        added = true;
1712 >                                        pred.next = new Node<K,V>(h, key, val);
1713 >                                    }
1714 >                                    break;
1715 >                                }
1716 >                            }
1717 >                        }
1718 >                        else if (f instanceof TreeBin) {
1719 >                            binCount = 2;
1720 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1721 >                            TreeNode<K,V> r, p;
1722 >                            if ((r = t.root) != null &&
1723 >                                (p = r.findTreeNode(h, key, null)) != null)
1724 >                                val = p.val;
1725 >                            else if ((val = mappingFunction.apply(key)) != null) {
1726 >                                added = true;
1727 >                                t.putTreeVal(h, key, val);
1728 >                            }
1729 >                        }
1730 >                        else if (f instanceof ReservationNode)
1731 >                            throw new IllegalStateException("Recursive update");
1732 >                    }
1733 >                }
1734 >                if (binCount != 0) {
1735 >                    if (binCount >= TREEIFY_THRESHOLD)
1736 >                        treeifyBin(tab, i);
1737 >                    if (!added)
1738 >                        return val;
1739 >                    break;
1740 >                }
1741 >            }
1742 >        }
1743 >        if (val != null)
1744 >            addCount(1L, binCount);
1745 >        return val;
1746 >    }
1747 >
1748 >    /**
1749 >     * If the value for the specified key is present, attempts to
1750 >     * compute a new mapping given the key and its current mapped
1751 >     * value.  The entire method invocation is performed atomically.
1752 >     * Some attempted update operations on this map by other threads
1753 >     * may be blocked while computation is in progress, so the
1754 >     * computation should be short and simple, and must not attempt to
1755 >     * update any other mappings of this map.
1756 >     *
1757 >     * @param key key with which a value may be associated
1758 >     * @param remappingFunction the function to compute a value
1759 >     * @return the new value associated with the specified key, or null if none
1760 >     * @throws NullPointerException if the specified key or remappingFunction
1761 >     *         is null
1762 >     * @throws IllegalStateException if the computation detectably
1763 >     *         attempts a recursive update to this map that would
1764 >     *         otherwise never complete
1765 >     * @throws RuntimeException or Error if the remappingFunction does so,
1766 >     *         in which case the mapping is unchanged
1767 >     */
1768 >    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1769 >        if (key == null || remappingFunction == null)
1770 >            throw new NullPointerException();
1771 >        int h = spread(key.hashCode());
1772 >        V val = null;
1773 >        int delta = 0;
1774 >        int binCount = 0;
1775 >        for (Node<K,V>[] tab = table;;) {
1776 >            Node<K,V> f; int n, i, fh;
1777 >            if (tab == null || (n = tab.length) == 0)
1778 >                tab = initTable();
1779 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1780 >                break;
1781 >            else if ((fh = f.hash) == MOVED)
1782 >                tab = helpTransfer(tab, f);
1783 >            else {
1784 >                synchronized (f) {
1785 >                    if (tabAt(tab, i) == f) {
1786 >                        if (fh >= 0) {
1787 >                            binCount = 1;
1788 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1789 >                                K ek;
1790 >                                if (e.hash == h &&
1791 >                                    ((ek = e.key) == key ||
1792 >                                     (ek != null && key.equals(ek)))) {
1793 >                                    val = remappingFunction.apply(key, e.val);
1794 >                                    if (val != null)
1795 >                                        e.val = val;
1796 >                                    else {
1797 >                                        delta = -1;
1798 >                                        Node<K,V> en = e.next;
1799 >                                        if (pred != null)
1800 >                                            pred.next = en;
1801 >                                        else
1802 >                                            setTabAt(tab, i, en);
1803 >                                    }
1804 >                                    break;
1805 >                                }
1806 >                                pred = e;
1807 >                                if ((e = e.next) == null)
1808 >                                    break;
1809 >                            }
1810 >                        }
1811 >                        else if (f instanceof TreeBin) {
1812 >                            binCount = 2;
1813 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1814 >                            TreeNode<K,V> r, p;
1815 >                            if ((r = t.root) != null &&
1816 >                                (p = r.findTreeNode(h, key, null)) != null) {
1817 >                                val = remappingFunction.apply(key, p.val);
1818 >                                if (val != null)
1819 >                                    p.val = val;
1820 >                                else {
1821 >                                    delta = -1;
1822 >                                    if (t.removeTreeNode(p))
1823 >                                        setTabAt(tab, i, untreeify(t.first));
1824 >                                }
1825 >                            }
1826 >                        }
1827 >                        else if (f instanceof ReservationNode)
1828 >                            throw new IllegalStateException("Recursive update");
1829 >                    }
1830 >                }
1831 >                if (binCount != 0)
1832 >                    break;
1833 >            }
1834 >        }
1835 >        if (delta != 0)
1836 >            addCount((long)delta, binCount);
1837 >        return val;
1838 >    }
1839 >
1840 >    /**
1841 >     * Attempts to compute a mapping for the specified key and its
1842 >     * current mapped value (or {@code null} if there is no current
1843 >     * mapping). The entire method invocation is performed atomically.
1844 >     * Some attempted update operations on this map by other threads
1845 >     * may be blocked while computation is in progress, so the
1846 >     * computation should be short and simple, and must not attempt to
1847 >     * update any other mappings of this Map.
1848 >     *
1849 >     * @param key key with which the specified value is to be associated
1850 >     * @param remappingFunction the function to compute a value
1851 >     * @return the new value associated with the specified key, or null if none
1852 >     * @throws NullPointerException if the specified key or remappingFunction
1853 >     *         is null
1854 >     * @throws IllegalStateException if the computation detectably
1855 >     *         attempts a recursive update to this map that would
1856 >     *         otherwise never complete
1857 >     * @throws RuntimeException or Error if the remappingFunction does so,
1858 >     *         in which case the mapping is unchanged
1859 >     */
1860 >    public V compute(K key,
1861 >                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
1862 >        if (key == null || remappingFunction == null)
1863 >            throw new NullPointerException();
1864 >        int h = spread(key.hashCode());
1865 >        V val = null;
1866 >        int delta = 0;
1867 >        int binCount = 0;
1868 >        for (Node<K,V>[] tab = table;;) {
1869 >            Node<K,V> f; int n, i, fh;
1870 >            if (tab == null || (n = tab.length) == 0)
1871 >                tab = initTable();
1872 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1873 >                Node<K,V> r = new ReservationNode<K,V>();
1874 >                synchronized (r) {
1875 >                    if (casTabAt(tab, i, null, r)) {
1876 >                        binCount = 1;
1877 >                        Node<K,V> node = null;
1878 >                        try {
1879 >                            if ((val = remappingFunction.apply(key, null)) != null) {
1880 >                                delta = 1;
1881 >                                node = new Node<K,V>(h, key, val);
1882 >                            }
1883 >                        } finally {
1884 >                            setTabAt(tab, i, node);
1885 >                        }
1886 >                    }
1887 >                }
1888 >                if (binCount != 0)
1889 >                    break;
1890 >            }
1891 >            else if ((fh = f.hash) == MOVED)
1892 >                tab = helpTransfer(tab, f);
1893 >            else {
1894 >                synchronized (f) {
1895 >                    if (tabAt(tab, i) == f) {
1896 >                        if (fh >= 0) {
1897 >                            binCount = 1;
1898 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
1899 >                                K ek;
1900 >                                if (e.hash == h &&
1901 >                                    ((ek = e.key) == key ||
1902 >                                     (ek != null && key.equals(ek)))) {
1903 >                                    val = remappingFunction.apply(key, e.val);
1904 >                                    if (val != null)
1905 >                                        e.val = val;
1906 >                                    else {
1907 >                                        delta = -1;
1908 >                                        Node<K,V> en = e.next;
1909 >                                        if (pred != null)
1910 >                                            pred.next = en;
1911 >                                        else
1912 >                                            setTabAt(tab, i, en);
1913 >                                    }
1914 >                                    break;
1915 >                                }
1916 >                                pred = e;
1917 >                                if ((e = e.next) == null) {
1918 >                                    val = remappingFunction.apply(key, null);
1919 >                                    if (val != null) {
1920 >                                        if (pred.next != null)
1921 >                                            throw new IllegalStateException("Recursive update");
1922 >                                        delta = 1;
1923 >                                        pred.next = new Node<K,V>(h, key, val);
1924 >                                    }
1925 >                                    break;
1926 >                                }
1927 >                            }
1928 >                        }
1929 >                        else if (f instanceof TreeBin) {
1930 >                            binCount = 1;
1931 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
1932 >                            TreeNode<K,V> r, p;
1933 >                            if ((r = t.root) != null)
1934 >                                p = r.findTreeNode(h, key, null);
1935 >                            else
1936 >                                p = null;
1937 >                            V pv = (p == null) ? null : p.val;
1938 >                            val = remappingFunction.apply(key, pv);
1939 >                            if (val != null) {
1940 >                                if (p != null)
1941 >                                    p.val = val;
1942 >                                else {
1943 >                                    delta = 1;
1944 >                                    t.putTreeVal(h, key, val);
1945 >                                }
1946 >                            }
1947 >                            else if (p != null) {
1948 >                                delta = -1;
1949 >                                if (t.removeTreeNode(p))
1950 >                                    setTabAt(tab, i, untreeify(t.first));
1951 >                            }
1952 >                        }
1953 >                        else if (f instanceof ReservationNode)
1954 >                            throw new IllegalStateException("Recursive update");
1955 >                    }
1956 >                }
1957 >                if (binCount != 0) {
1958 >                    if (binCount >= TREEIFY_THRESHOLD)
1959 >                        treeifyBin(tab, i);
1960 >                    break;
1961 >                }
1962 >            }
1963 >        }
1964 >        if (delta != 0)
1965 >            addCount((long)delta, binCount);
1966 >        return val;
1967 >    }
1968 >
1969 >    /**
1970 >     * If the specified key is not already associated with a
1971 >     * (non-null) value, associates it with the given value.
1972 >     * Otherwise, replaces the value with the results of the given
1973 >     * remapping function, or removes if {@code null}. The entire
1974 >     * method invocation is performed atomically.  Some attempted
1975 >     * update operations on this map by other threads may be blocked
1976 >     * while computation is in progress, so the computation should be
1977 >     * short and simple, and must not attempt to update any other
1978 >     * mappings of this Map.
1979 >     *
1980 >     * @param key key with which the specified value is to be associated
1981 >     * @param value the value to use if absent
1982 >     * @param remappingFunction the function to recompute a value if present
1983 >     * @return the new value associated with the specified key, or null if none
1984 >     * @throws NullPointerException if the specified key or the
1985 >     *         remappingFunction is null
1986 >     * @throws RuntimeException or Error if the remappingFunction does so,
1987 >     *         in which case the mapping is unchanged
1988 >     */
1989 >    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1990 >        if (key == null || value == null || remappingFunction == null)
1991 >            throw new NullPointerException();
1992 >        int h = spread(key.hashCode());
1993 >        V val = null;
1994 >        int delta = 0;
1995 >        int binCount = 0;
1996 >        for (Node<K,V>[] tab = table;;) {
1997 >            Node<K,V> f; int n, i, fh;
1998 >            if (tab == null || (n = tab.length) == 0)
1999 >                tab = initTable();
2000 >            else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
2001 >                if (casTabAt(tab, i, null, new Node<K,V>(h, key, value))) {
2002 >                    delta = 1;
2003 >                    val = value;
2004 >                    break;
2005 >                }
2006 >            }
2007 >            else if ((fh = f.hash) == MOVED)
2008 >                tab = helpTransfer(tab, f);
2009 >            else {
2010 >                synchronized (f) {
2011 >                    if (tabAt(tab, i) == f) {
2012 >                        if (fh >= 0) {
2013 >                            binCount = 1;
2014 >                            for (Node<K,V> e = f, pred = null;; ++binCount) {
2015 >                                K ek;
2016 >                                if (e.hash == h &&
2017 >                                    ((ek = e.key) == key ||
2018 >                                     (ek != null && key.equals(ek)))) {
2019 >                                    val = remappingFunction.apply(e.val, value);
2020 >                                    if (val != null)
2021 >                                        e.val = val;
2022 >                                    else {
2023 >                                        delta = -1;
2024 >                                        Node<K,V> en = e.next;
2025 >                                        if (pred != null)
2026 >                                            pred.next = en;
2027 >                                        else
2028 >                                            setTabAt(tab, i, en);
2029 >                                    }
2030 >                                    break;
2031 >                                }
2032 >                                pred = e;
2033 >                                if ((e = e.next) == null) {
2034 >                                    delta = 1;
2035 >                                    val = value;
2036 >                                    pred.next = new Node<K,V>(h, key, val);
2037 >                                    break;
2038 >                                }
2039 >                            }
2040 >                        }
2041 >                        else if (f instanceof TreeBin) {
2042 >                            binCount = 2;
2043 >                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2044 >                            TreeNode<K,V> r = t.root;
2045 >                            TreeNode<K,V> p = (r == null) ? null :
2046 >                                r.findTreeNode(h, key, null);
2047 >                            val = (p == null) ? value :
2048 >                                remappingFunction.apply(p.val, value);
2049 >                            if (val != null) {
2050 >                                if (p != null)
2051 >                                    p.val = val;
2052 >                                else {
2053 >                                    delta = 1;
2054 >                                    t.putTreeVal(h, key, val);
2055 >                                }
2056 >                            }
2057 >                            else if (p != null) {
2058 >                                delta = -1;
2059 >                                if (t.removeTreeNode(p))
2060 >                                    setTabAt(tab, i, untreeify(t.first));
2061 >                            }
2062 >                        }
2063 >                        else if (f instanceof ReservationNode)
2064 >                            throw new IllegalStateException("Recursive update");
2065 >                    }
2066 >                }
2067 >                if (binCount != 0) {
2068 >                    if (binCount >= TREEIFY_THRESHOLD)
2069 >                        treeifyBin(tab, i);
2070 >                    break;
2071 >                }
2072 >            }
2073 >        }
2074 >        if (delta != 0)
2075 >            addCount((long)delta, binCount);
2076 >        return val;
2077 >    }
2078 >
2079 >    // Hashtable legacy methods
2080 >
2081 >    /**
2082 >     * Tests if some key maps into the specified value in this table.
2083 >     *
2084 >     * <p>Note that this method is identical in functionality to
2085 >     * {@link #containsValue(Object)}, and exists solely to ensure
2086 >     * full compatibility with class {@link java.util.Hashtable},
2087 >     * which supported this method prior to introduction of the
2088 >     * Java Collections Framework.
2089 >     *
2090 >     * @param  value a value to search for
2091 >     * @return {@code true} if and only if some key maps to the
2092 >     *         {@code value} argument in this table as
2093 >     *         determined by the {@code equals} method;
2094 >     *         {@code false} otherwise
2095 >     * @throws NullPointerException if the specified value is null
2096 >     */
2097 >    public boolean contains(Object value) {
2098 >        return containsValue(value);
2099      }
2100  
2101      /**
2102       * Returns an enumeration of the keys in this table.
2103       *
2104       * @return an enumeration of the keys in this table
2105 <     * @see #keySet
2105 >     * @see #keySet()
2106       */
2107      public Enumeration<K> keys() {
2108 <        return new KeyIterator();
2108 >        Node<K,V>[] t;
2109 >        int f = (t = table) == null ? 0 : t.length;
2110 >        return new KeyIterator<K,V>(t, f, 0, f, this);
2111      }
2112  
2113      /**
2114       * Returns an enumeration of the values in this table.
2115       *
2116       * @return an enumeration of the values in this table
2117 <     * @see #values
2117 >     * @see #values()
2118       */
2119      public Enumeration<V> elements() {
2120 <        return new ValueIterator();
2120 >        Node<K,V>[] t;
2121 >        int f = (t = table) == null ? 0 : t.length;
2122 >        return new ValueIterator<K,V>(t, f, 0, f, this);
2123      }
2124  
2125 <    /* ---------------- Iterator Support -------------- */
2125 >    // ConcurrentHashMap-only methods
2126  
2127 <    abstract class HashIterator {
2128 <        int nextSegmentIndex;
2129 <        int nextTableIndex;
2130 <        HashEntry<K,V>[] currentTable;
2131 <        HashEntry<K, V> nextEntry;
2132 <        HashEntry<K, V> lastReturned;
2127 >    /**
2128 >     * Returns the number of mappings. This method should be used
2129 >     * instead of {@link #size} because a ConcurrentHashMap may
2130 >     * contain more mappings than can be represented as an int. The
2131 >     * value returned is an estimate; the actual count may differ if
2132 >     * there are concurrent insertions or removals.
2133 >     *
2134 >     * @return the number of mappings
2135 >     * @since 1.8
2136 >     */
2137 >    public long mappingCount() {
2138 >        long n = sumCount();
2139 >        return (n < 0L) ? 0L : n; // ignore transient negative values
2140 >    }
2141  
2142 <        HashIterator() {
2143 <            nextSegmentIndex = segments.length - 1;
2144 <            nextTableIndex = -1;
2145 <            advance();
2142 >    /**
2143 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2144 >     * from the given type to {@code Boolean.TRUE}.
2145 >     *
2146 >     * @param <K> the element type of the returned set
2147 >     * @return the new set
2148 >     * @since 1.8
2149 >     */
2150 >    public static <K> KeySetView<K,Boolean> newKeySet() {
2151 >        return new KeySetView<K,Boolean>
2152 >            (new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE);
2153 >    }
2154 >
2155 >    /**
2156 >     * Creates a new {@link Set} backed by a ConcurrentHashMap
2157 >     * from the given type to {@code Boolean.TRUE}.
2158 >     *
2159 >     * @param initialCapacity The implementation performs internal
2160 >     * sizing to accommodate this many elements.
2161 >     * @param <K> the element type of the returned set
2162 >     * @return the new set
2163 >     * @throws IllegalArgumentException if the initial capacity of
2164 >     * elements is negative
2165 >     * @since 1.8
2166 >     */
2167 >    public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2168 >        return new KeySetView<K,Boolean>
2169 >            (new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE);
2170 >    }
2171 >
2172 >    /**
2173 >     * Returns a {@link Set} view of the keys in this map, using the
2174 >     * given common mapped value for any additions (i.e., {@link
2175 >     * Collection#add} and {@link Collection#addAll(Collection)}).
2176 >     * This is of course only appropriate if it is acceptable to use
2177 >     * the same value for all additions from this view.
2178 >     *
2179 >     * @param mappedValue the mapped value to use for any additions
2180 >     * @return the set view
2181 >     * @throws NullPointerException if the mappedValue is null
2182 >     */
2183 >    public KeySetView<K,V> keySet(V mappedValue) {
2184 >        if (mappedValue == null)
2185 >            throw new NullPointerException();
2186 >        return new KeySetView<K,V>(this, mappedValue);
2187 >    }
2188 >
2189 >    /* ---------------- Special Nodes -------------- */
2190 >
2191 >    /**
2192 >     * A node inserted at head of bins during transfer operations.
2193 >     */
2194 >    static final class ForwardingNode<K,V> extends Node<K,V> {
2195 >        final Node<K,V>[] nextTable;
2196 >        ForwardingNode(Node<K,V>[] tab) {
2197 >            super(MOVED, null, null);
2198 >            this.nextTable = tab;
2199 >        }
2200 >
2201 >        Node<K,V> find(int h, Object k) {
2202 >            // loop to avoid arbitrarily deep recursion on forwarding nodes
2203 >            outer: for (Node<K,V>[] tab = nextTable;;) {
2204 >                Node<K,V> e; int n;
2205 >                if (k == null || tab == null || (n = tab.length) == 0 ||
2206 >                    (e = tabAt(tab, (n - 1) & h)) == null)
2207 >                    return null;
2208 >                for (;;) {
2209 >                    int eh; K ek;
2210 >                    if ((eh = e.hash) == h &&
2211 >                        ((ek = e.key) == k || (ek != null && k.equals(ek))))
2212 >                        return e;
2213 >                    if (eh < 0) {
2214 >                        if (e instanceof ForwardingNode) {
2215 >                            tab = ((ForwardingNode<K,V>)e).nextTable;
2216 >                            continue outer;
2217 >                        }
2218 >                        else
2219 >                            return e.find(h, k);
2220 >                    }
2221 >                    if ((e = e.next) == null)
2222 >                        return null;
2223 >                }
2224 >            }
2225          }
2226 +    }
2227 +
2228 +    /**
2229 +     * A place-holder node used in computeIfAbsent and compute.
2230 +     */
2231 +    static final class ReservationNode<K,V> extends Node<K,V> {
2232 +        ReservationNode() {
2233 +            super(RESERVED, null, null);
2234 +        }
2235 +
2236 +        Node<K,V> find(int h, Object k) {
2237 +            return null;
2238 +        }
2239 +    }
2240 +
2241 +    /* ---------------- Table Initialization and Resizing -------------- */
2242 +
2243 +    /**
2244 +     * Returns the stamp bits for resizing a table of size n.
2245 +     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2246 +     */
2247 +    static final int resizeStamp(int n) {
2248 +        return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2249 +    }
2250  
2251 <        public boolean hasMoreElements() { return hasNext(); }
2251 >    /**
2252 >     * Initializes table, using the size recorded in sizeCtl.
2253 >     */
2254 >    private final Node<K,V>[] initTable() {
2255 >        Node<K,V>[] tab; int sc;
2256 >        while ((tab = table) == null || tab.length == 0) {
2257 >            if ((sc = sizeCtl) < 0)
2258 >                Thread.yield(); // lost initialization race; just spin
2259 >            else if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
2260 >                try {
2261 >                    if ((tab = table) == null || tab.length == 0) {
2262 >                        int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2263 >                        @SuppressWarnings("unchecked")
2264 >                        Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2265 >                        table = tab = nt;
2266 >                        sc = n - (n >>> 2);
2267 >                    }
2268 >                } finally {
2269 >                    sizeCtl = sc;
2270 >                }
2271 >                break;
2272 >            }
2273 >        }
2274 >        return tab;
2275 >    }
2276  
2277 <        final void advance() {
2278 <            if (nextEntry != null && (nextEntry = nextEntry.next) != null)
2277 >    /**
2278 >     * Adds to count, and if table is too small and not already
2279 >     * resizing, initiates transfer. If already resizing, helps
2280 >     * perform transfer if work is available.  Rechecks occupancy
2281 >     * after a transfer to see if another resize is already needed
2282 >     * because resizings are lagging additions.
2283 >     *
2284 >     * @param x the count to add
2285 >     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2286 >     */
2287 >    private final void addCount(long x, int check) {
2288 >        CounterCell[] cs; long b, s;
2289 >        if ((cs = counterCells) != null ||
2290 >            !U.compareAndSetLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2291 >            CounterCell c; long v; int m;
2292 >            boolean uncontended = true;
2293 >            if (cs == null || (m = cs.length - 1) < 0 ||
2294 >                (c = cs[ThreadLocalRandom.getProbe() & m]) == null ||
2295 >                !(uncontended =
2296 >                  U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))) {
2297 >                fullAddCount(x, uncontended);
2298 >                return;
2299 >            }
2300 >            if (check <= 1)
2301                  return;
2302 +            s = sumCount();
2303 +        }
2304 +        if (check >= 0) {
2305 +            Node<K,V>[] tab, nt; int n, sc;
2306 +            while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2307 +                   (n = tab.length) < MAXIMUM_CAPACITY) {
2308 +                int rs = resizeStamp(n) << RESIZE_STAMP_SHIFT;
2309 +                if (sc < 0) {
2310 +                    if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
2311 +                        (nt = nextTable) == null || transferIndex <= 0)
2312 +                        break;
2313 +                    if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1))
2314 +                        transfer(tab, nt);
2315 +                }
2316 +                else if (U.compareAndSetInt(this, SIZECTL, sc, rs + 2))
2317 +                    transfer(tab, null);
2318 +                s = sumCount();
2319 +            }
2320 +        }
2321 +    }
2322 +
2323 +    /**
2324 +     * Helps transfer if a resize is in progress.
2325 +     */
2326 +    final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2327 +        Node<K,V>[] nextTab; int sc;
2328 +        if (tab != null && (f instanceof ForwardingNode) &&
2329 +            (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2330 +            int rs = resizeStamp(tab.length) << RESIZE_STAMP_SHIFT;
2331 +            while (nextTab == nextTable && table == tab &&
2332 +                   (sc = sizeCtl) < 0) {
2333 +                if (sc == rs + MAX_RESIZERS || sc == rs + 1 ||
2334 +                    transferIndex <= 0)
2335 +                    break;
2336 +                if (U.compareAndSetInt(this, SIZECTL, sc, sc + 1)) {
2337 +                    transfer(tab, nextTab);
2338 +                    break;
2339 +                }
2340 +            }
2341 +            return nextTab;
2342 +        }
2343 +        return table;
2344 +    }
2345  
2346 <            while (nextTableIndex >= 0) {
2347 <                if ( (nextEntry = currentTable[nextTableIndex--]) != null)
2346 >    /**
2347 >     * Tries to presize table to accommodate the given number of elements.
2348 >     *
2349 >     * @param size number of elements (doesn't need to be perfectly accurate)
2350 >     */
2351 >    private final void tryPresize(int size) {
2352 >        int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2353 >            tableSizeFor(size + (size >>> 1) + 1);
2354 >        int sc;
2355 >        while ((sc = sizeCtl) >= 0) {
2356 >            Node<K,V>[] tab = table; int n;
2357 >            if (tab == null || (n = tab.length) == 0) {
2358 >                n = (sc > c) ? sc : c;
2359 >                if (U.compareAndSetInt(this, SIZECTL, sc, -1)) {
2360 >                    try {
2361 >                        if (table == tab) {
2362 >                            @SuppressWarnings("unchecked")
2363 >                            Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2364 >                            table = nt;
2365 >                            sc = n - (n >>> 2);
2366 >                        }
2367 >                    } finally {
2368 >                        sizeCtl = sc;
2369 >                    }
2370 >                }
2371 >            }
2372 >            else if (c <= sc || n >= MAXIMUM_CAPACITY)
2373 >                break;
2374 >            else if (tab == table) {
2375 >                int rs = resizeStamp(n);
2376 >                if (U.compareAndSetInt(this, SIZECTL, sc,
2377 >                                        (rs << RESIZE_STAMP_SHIFT) + 2))
2378 >                    transfer(tab, null);
2379 >            }
2380 >        }
2381 >    }
2382 >
2383 >    /**
2384 >     * Moves and/or copies the nodes in each bin to new table. See
2385 >     * above for explanation.
2386 >     */
2387 >    private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2388 >        int n = tab.length, stride;
2389 >        if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2390 >            stride = MIN_TRANSFER_STRIDE; // subdivide range
2391 >        if (nextTab == null) {            // initiating
2392 >            try {
2393 >                @SuppressWarnings("unchecked")
2394 >                Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2395 >                nextTab = nt;
2396 >            } catch (Throwable ex) {      // try to cope with OOME
2397 >                sizeCtl = Integer.MAX_VALUE;
2398 >                return;
2399 >            }
2400 >            nextTable = nextTab;
2401 >            transferIndex = n;
2402 >        }
2403 >        int nextn = nextTab.length;
2404 >        ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2405 >        boolean advance = true;
2406 >        boolean finishing = false; // to ensure sweep before committing nextTab
2407 >        for (int i = 0, bound = 0;;) {
2408 >            Node<K,V> f; int fh;
2409 >            while (advance) {
2410 >                int nextIndex, nextBound;
2411 >                if (--i >= bound || finishing)
2412 >                    advance = false;
2413 >                else if ((nextIndex = transferIndex) <= 0) {
2414 >                    i = -1;
2415 >                    advance = false;
2416 >                }
2417 >                else if (U.compareAndSetInt
2418 >                         (this, TRANSFERINDEX, nextIndex,
2419 >                          nextBound = (nextIndex > stride ?
2420 >                                       nextIndex - stride : 0))) {
2421 >                    bound = nextBound;
2422 >                    i = nextIndex - 1;
2423 >                    advance = false;
2424 >                }
2425 >            }
2426 >            if (i < 0 || i >= n || i + n >= nextn) {
2427 >                int sc;
2428 >                if (finishing) {
2429 >                    nextTable = null;
2430 >                    table = nextTab;
2431 >                    sizeCtl = (n << 1) - (n >>> 1);
2432                      return;
2433 +                }
2434 +                if (U.compareAndSetInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2435 +                    if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2436 +                        return;
2437 +                    finishing = advance = true;
2438 +                    i = n; // recheck before commit
2439 +                }
2440 +            }
2441 +            else if ((f = tabAt(tab, i)) == null)
2442 +                advance = casTabAt(tab, i, null, fwd);
2443 +            else if ((fh = f.hash) == MOVED)
2444 +                advance = true; // already processed
2445 +            else {
2446 +                synchronized (f) {
2447 +                    if (tabAt(tab, i) == f) {
2448 +                        Node<K,V> ln, hn;
2449 +                        if (fh >= 0) {
2450 +                            int runBit = fh & n;
2451 +                            Node<K,V> lastRun = f;
2452 +                            for (Node<K,V> p = f.next; p != null; p = p.next) {
2453 +                                int b = p.hash & n;
2454 +                                if (b != runBit) {
2455 +                                    runBit = b;
2456 +                                    lastRun = p;
2457 +                                }
2458 +                            }
2459 +                            if (runBit == 0) {
2460 +                                ln = lastRun;
2461 +                                hn = null;
2462 +                            }
2463 +                            else {
2464 +                                hn = lastRun;
2465 +                                ln = null;
2466 +                            }
2467 +                            for (Node<K,V> p = f; p != lastRun; p = p.next) {
2468 +                                int ph = p.hash; K pk = p.key; V pv = p.val;
2469 +                                if ((ph & n) == 0)
2470 +                                    ln = new Node<K,V>(ph, pk, pv, ln);
2471 +                                else
2472 +                                    hn = new Node<K,V>(ph, pk, pv, hn);
2473 +                            }
2474 +                            setTabAt(nextTab, i, ln);
2475 +                            setTabAt(nextTab, i + n, hn);
2476 +                            setTabAt(tab, i, fwd);
2477 +                            advance = true;
2478 +                        }
2479 +                        else if (f instanceof TreeBin) {
2480 +                            TreeBin<K,V> t = (TreeBin<K,V>)f;
2481 +                            TreeNode<K,V> lo = null, loTail = null;
2482 +                            TreeNode<K,V> hi = null, hiTail = null;
2483 +                            int lc = 0, hc = 0;
2484 +                            for (Node<K,V> e = t.first; e != null; e = e.next) {
2485 +                                int h = e.hash;
2486 +                                TreeNode<K,V> p = new TreeNode<K,V>
2487 +                                    (h, e.key, e.val, null, null);
2488 +                                if ((h & n) == 0) {
2489 +                                    if ((p.prev = loTail) == null)
2490 +                                        lo = p;
2491 +                                    else
2492 +                                        loTail.next = p;
2493 +                                    loTail = p;
2494 +                                    ++lc;
2495 +                                }
2496 +                                else {
2497 +                                    if ((p.prev = hiTail) == null)
2498 +                                        hi = p;
2499 +                                    else
2500 +                                        hiTail.next = p;
2501 +                                    hiTail = p;
2502 +                                    ++hc;
2503 +                                }
2504 +                            }
2505 +                            ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2506 +                                (hc != 0) ? new TreeBin<K,V>(lo) : t;
2507 +                            hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2508 +                                (lc != 0) ? new TreeBin<K,V>(hi) : t;
2509 +                            setTabAt(nextTab, i, ln);
2510 +                            setTabAt(nextTab, i + n, hn);
2511 +                            setTabAt(tab, i, fwd);
2512 +                            advance = true;
2513 +                        }
2514 +                        else if (f instanceof ReservationNode)
2515 +                            throw new IllegalStateException("Recursive update");
2516 +                    }
2517 +                }
2518 +            }
2519 +        }
2520 +    }
2521 +
2522 +    /* ---------------- Counter support -------------- */
2523 +
2524 +    /**
2525 +     * A padded cell for distributing counts.  Adapted from LongAdder
2526 +     * and Striped64.  See their internal docs for explanation.
2527 +     */
2528 +    @jdk.internal.vm.annotation.Contended static final class CounterCell {
2529 +        volatile long value;
2530 +        CounterCell(long x) { value = x; }
2531 +    }
2532 +
2533 +    final long sumCount() {
2534 +        CounterCell[] cs = counterCells;
2535 +        long sum = baseCount;
2536 +        if (cs != null) {
2537 +            for (CounterCell c : cs)
2538 +                if (c != null)
2539 +                    sum += c.value;
2540 +        }
2541 +        return sum;
2542 +    }
2543 +
2544 +    // See LongAdder version for explanation
2545 +    private final void fullAddCount(long x, boolean wasUncontended) {
2546 +        int h;
2547 +        if ((h = ThreadLocalRandom.getProbe()) == 0) {
2548 +            ThreadLocalRandom.localInit();      // force initialization
2549 +            h = ThreadLocalRandom.getProbe();
2550 +            wasUncontended = true;
2551 +        }
2552 +        boolean collide = false;                // True if last slot nonempty
2553 +        for (;;) {
2554 +            CounterCell[] cs; CounterCell c; int n; long v;
2555 +            if ((cs = counterCells) != null && (n = cs.length) > 0) {
2556 +                if ((c = cs[(n - 1) & h]) == null) {
2557 +                    if (cellsBusy == 0) {            // Try to attach new Cell
2558 +                        CounterCell r = new CounterCell(x); // Optimistic create
2559 +                        if (cellsBusy == 0 &&
2560 +                            U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2561 +                            boolean created = false;
2562 +                            try {               // Recheck under lock
2563 +                                CounterCell[] rs; int m, j;
2564 +                                if ((rs = counterCells) != null &&
2565 +                                    (m = rs.length) > 0 &&
2566 +                                    rs[j = (m - 1) & h] == null) {
2567 +                                    rs[j] = r;
2568 +                                    created = true;
2569 +                                }
2570 +                            } finally {
2571 +                                cellsBusy = 0;
2572 +                            }
2573 +                            if (created)
2574 +                                break;
2575 +                            continue;           // Slot is now non-empty
2576 +                        }
2577 +                    }
2578 +                    collide = false;
2579 +                }
2580 +                else if (!wasUncontended)       // CAS already known to fail
2581 +                    wasUncontended = true;      // Continue after rehash
2582 +                else if (U.compareAndSetLong(c, CELLVALUE, v = c.value, v + x))
2583 +                    break;
2584 +                else if (counterCells != cs || n >= NCPU)
2585 +                    collide = false;            // At max size or stale
2586 +                else if (!collide)
2587 +                    collide = true;
2588 +                else if (cellsBusy == 0 &&
2589 +                         U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2590 +                    try {
2591 +                        if (counterCells == cs) // Expand table unless stale
2592 +                            counterCells = Arrays.copyOf(cs, n << 1);
2593 +                    } finally {
2594 +                        cellsBusy = 0;
2595 +                    }
2596 +                    collide = false;
2597 +                    continue;                   // Retry with expanded table
2598 +                }
2599 +                h = ThreadLocalRandom.advanceProbe(h);
2600 +            }
2601 +            else if (cellsBusy == 0 && counterCells == cs &&
2602 +                     U.compareAndSetInt(this, CELLSBUSY, 0, 1)) {
2603 +                boolean init = false;
2604 +                try {                           // Initialize table
2605 +                    if (counterCells == cs) {
2606 +                        CounterCell[] rs = new CounterCell[2];
2607 +                        rs[h & 1] = new CounterCell(x);
2608 +                        counterCells = rs;
2609 +                        init = true;
2610 +                    }
2611 +                } finally {
2612 +                    cellsBusy = 0;
2613 +                }
2614 +                if (init)
2615 +                    break;
2616              }
2617 +            else if (U.compareAndSetLong(this, BASECOUNT, v = baseCount, v + x))
2618 +                break;                          // Fall back on using base
2619 +        }
2620 +    }
2621 +
2622 +    /* ---------------- Conversion from/to TreeBins -------------- */
2623  
2624 <            while (nextSegmentIndex >= 0) {
2625 <                Segment<K,V> seg = segments[nextSegmentIndex--];
2626 <                if (seg.count != 0) {
2627 <                    currentTable = seg.table;
2628 <                    for (int j = currentTable.length - 1; j >= 0; --j) {
2629 <                        if ( (nextEntry = currentTable[j]) != null) {
2630 <                            nextTableIndex = j - 1;
2631 <                            return;
2624 >    /**
2625 >     * Replaces all linked nodes in bin at given index unless table is
2626 >     * too small, in which case resizes instead.
2627 >     */
2628 >    private final void treeifyBin(Node<K,V>[] tab, int index) {
2629 >        Node<K,V> b; int n;
2630 >        if (tab != null) {
2631 >            if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2632 >                tryPresize(n << 1);
2633 >            else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2634 >                synchronized (b) {
2635 >                    if (tabAt(tab, index) == b) {
2636 >                        TreeNode<K,V> hd = null, tl = null;
2637 >                        for (Node<K,V> e = b; e != null; e = e.next) {
2638 >                            TreeNode<K,V> p =
2639 >                                new TreeNode<K,V>(e.hash, e.key, e.val,
2640 >                                                  null, null);
2641 >                            if ((p.prev = tl) == null)
2642 >                                hd = p;
2643 >                            else
2644 >                                tl.next = p;
2645 >                            tl = p;
2646                          }
2647 +                        setTabAt(tab, index, new TreeBin<K,V>(hd));
2648                      }
2649                  }
2650              }
2651          }
2652 +    }
2653  
2654 <        public boolean hasNext() { return nextEntry != null; }
2654 >    /**
2655 >     * Returns a list of non-TreeNodes replacing those in given list.
2656 >     */
2657 >    static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2658 >        Node<K,V> hd = null, tl = null;
2659 >        for (Node<K,V> q = b; q != null; q = q.next) {
2660 >            Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val);
2661 >            if (tl == null)
2662 >                hd = p;
2663 >            else
2664 >                tl.next = p;
2665 >            tl = p;
2666 >        }
2667 >        return hd;
2668 >    }
2669  
2670 <        HashEntry<K,V> nextEntry() {
2671 <            if (nextEntry == null)
2672 <                throw new NoSuchElementException();
2673 <            lastReturned = nextEntry;
2670 >    /* ---------------- TreeNodes -------------- */
2671 >
2672 >    /**
2673 >     * Nodes for use in TreeBins.
2674 >     */
2675 >    static final class TreeNode<K,V> extends Node<K,V> {
2676 >        TreeNode<K,V> parent;  // red-black tree links
2677 >        TreeNode<K,V> left;
2678 >        TreeNode<K,V> right;
2679 >        TreeNode<K,V> prev;    // needed to unlink next upon deletion
2680 >        boolean red;
2681 >
2682 >        TreeNode(int hash, K key, V val, Node<K,V> next,
2683 >                 TreeNode<K,V> parent) {
2684 >            super(hash, key, val, next);
2685 >            this.parent = parent;
2686 >        }
2687 >
2688 >        Node<K,V> find(int h, Object k) {
2689 >            return findTreeNode(h, k, null);
2690 >        }
2691 >
2692 >        /**
2693 >         * Returns the TreeNode (or null if not found) for the given key
2694 >         * starting at given root.
2695 >         */
2696 >        final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2697 >            if (k != null) {
2698 >                TreeNode<K,V> p = this;
2699 >                do {
2700 >                    int ph, dir; K pk; TreeNode<K,V> q;
2701 >                    TreeNode<K,V> pl = p.left, pr = p.right;
2702 >                    if ((ph = p.hash) > h)
2703 >                        p = pl;
2704 >                    else if (ph < h)
2705 >                        p = pr;
2706 >                    else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2707 >                        return p;
2708 >                    else if (pl == null)
2709 >                        p = pr;
2710 >                    else if (pr == null)
2711 >                        p = pl;
2712 >                    else if ((kc != null ||
2713 >                              (kc = comparableClassFor(k)) != null) &&
2714 >                             (dir = compareComparables(kc, k, pk)) != 0)
2715 >                        p = (dir < 0) ? pl : pr;
2716 >                    else if ((q = pr.findTreeNode(h, k, kc)) != null)
2717 >                        return q;
2718 >                    else
2719 >                        p = pl;
2720 >                } while (p != null);
2721 >            }
2722 >            return null;
2723 >        }
2724 >    }
2725 >
2726 >    /* ---------------- TreeBins -------------- */
2727 >
2728 >    /**
2729 >     * TreeNodes used at the heads of bins. TreeBins do not hold user
2730 >     * keys or values, but instead point to list of TreeNodes and
2731 >     * their root. They also maintain a parasitic read-write lock
2732 >     * forcing writers (who hold bin lock) to wait for readers (who do
2733 >     * not) to complete before tree restructuring operations.
2734 >     */
2735 >    static final class TreeBin<K,V> extends Node<K,V> {
2736 >        TreeNode<K,V> root;
2737 >        volatile TreeNode<K,V> first;
2738 >        volatile Thread waiter;
2739 >        volatile int lockState;
2740 >        // values for lockState
2741 >        static final int WRITER = 1; // set while holding write lock
2742 >        static final int WAITER = 2; // set when waiting for write lock
2743 >        static final int READER = 4; // increment value for setting read lock
2744 >
2745 >        /**
2746 >         * Tie-breaking utility for ordering insertions when equal
2747 >         * hashCodes and non-comparable. We don't require a total
2748 >         * order, just a consistent insertion rule to maintain
2749 >         * equivalence across rebalancings. Tie-breaking further than
2750 >         * necessary simplifies testing a bit.
2751 >         */
2752 >        static int tieBreakOrder(Object a, Object b) {
2753 >            int d;
2754 >            if (a == null || b == null ||
2755 >                (d = a.getClass().getName().
2756 >                 compareTo(b.getClass().getName())) == 0)
2757 >                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
2758 >                     -1 : 1);
2759 >            return d;
2760 >        }
2761 >
2762 >        /**
2763 >         * Creates bin with initial set of nodes headed by b.
2764 >         */
2765 >        TreeBin(TreeNode<K,V> b) {
2766 >            super(TREEBIN, null, null);
2767 >            this.first = b;
2768 >            TreeNode<K,V> r = null;
2769 >            for (TreeNode<K,V> x = b, next; x != null; x = next) {
2770 >                next = (TreeNode<K,V>)x.next;
2771 >                x.left = x.right = null;
2772 >                if (r == null) {
2773 >                    x.parent = null;
2774 >                    x.red = false;
2775 >                    r = x;
2776 >                }
2777 >                else {
2778 >                    K k = x.key;
2779 >                    int h = x.hash;
2780 >                    Class<?> kc = null;
2781 >                    for (TreeNode<K,V> p = r;;) {
2782 >                        int dir, ph;
2783 >                        K pk = p.key;
2784 >                        if ((ph = p.hash) > h)
2785 >                            dir = -1;
2786 >                        else if (ph < h)
2787 >                            dir = 1;
2788 >                        else if ((kc == null &&
2789 >                                  (kc = comparableClassFor(k)) == null) ||
2790 >                                 (dir = compareComparables(kc, k, pk)) == 0)
2791 >                            dir = tieBreakOrder(k, pk);
2792 >                        TreeNode<K,V> xp = p;
2793 >                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
2794 >                            x.parent = xp;
2795 >                            if (dir <= 0)
2796 >                                xp.left = x;
2797 >                            else
2798 >                                xp.right = x;
2799 >                            r = balanceInsertion(r, x);
2800 >                            break;
2801 >                        }
2802 >                    }
2803 >                }
2804 >            }
2805 >            this.root = r;
2806 >            assert checkInvariants(root);
2807 >        }
2808 >
2809 >        /**
2810 >         * Acquires write lock for tree restructuring.
2811 >         */
2812 >        private final void lockRoot() {
2813 >            if (!U.compareAndSetInt(this, LOCKSTATE, 0, WRITER))
2814 >                contendedLock(); // offload to separate method
2815 >        }
2816 >
2817 >        /**
2818 >         * Releases write lock for tree restructuring.
2819 >         */
2820 >        private final void unlockRoot() {
2821 >            lockState = 0;
2822 >        }
2823 >
2824 >        /**
2825 >         * Possibly blocks awaiting root lock.
2826 >         */
2827 >        private final void contendedLock() {
2828 >            boolean waiting = false;
2829 >            for (int s;;) {
2830 >                if (((s = lockState) & ~WAITER) == 0) {
2831 >                    if (U.compareAndSetInt(this, LOCKSTATE, s, WRITER)) {
2832 >                        if (waiting)
2833 >                            waiter = null;
2834 >                        return;
2835 >                    }
2836 >                }
2837 >                else if ((s & WAITER) == 0) {
2838 >                    if (U.compareAndSetInt(this, LOCKSTATE, s, s | WAITER)) {
2839 >                        waiting = true;
2840 >                        waiter = Thread.currentThread();
2841 >                    }
2842 >                }
2843 >                else if (waiting)
2844 >                    LockSupport.park(this);
2845 >            }
2846 >        }
2847 >
2848 >        /**
2849 >         * Returns matching node or null if none. Tries to search
2850 >         * using tree comparisons from root, but continues linear
2851 >         * search when lock not available.
2852 >         */
2853 >        final Node<K,V> find(int h, Object k) {
2854 >            if (k != null) {
2855 >                for (Node<K,V> e = first; e != null; ) {
2856 >                    int s; K ek;
2857 >                    if (((s = lockState) & (WAITER|WRITER)) != 0) {
2858 >                        if (e.hash == h &&
2859 >                            ((ek = e.key) == k || (ek != null && k.equals(ek))))
2860 >                            return e;
2861 >                        e = e.next;
2862 >                    }
2863 >                    else if (U.compareAndSetInt(this, LOCKSTATE, s,
2864 >                                                 s + READER)) {
2865 >                        TreeNode<K,V> r, p;
2866 >                        try {
2867 >                            p = ((r = root) == null ? null :
2868 >                                 r.findTreeNode(h, k, null));
2869 >                        } finally {
2870 >                            Thread w;
2871 >                            if (U.getAndAddInt(this, LOCKSTATE, -READER) ==
2872 >                                (READER|WAITER) && (w = waiter) != null)
2873 >                                LockSupport.unpark(w);
2874 >                        }
2875 >                        return p;
2876 >                    }
2877 >                }
2878 >            }
2879 >            return null;
2880 >        }
2881 >
2882 >        /**
2883 >         * Finds or adds a node.
2884 >         * @return null if added
2885 >         */
2886 >        final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2887 >            Class<?> kc = null;
2888 >            boolean searched = false;
2889 >            for (TreeNode<K,V> p = root;;) {
2890 >                int dir, ph; K pk;
2891 >                if (p == null) {
2892 >                    first = root = new TreeNode<K,V>(h, k, v, null, null);
2893 >                    break;
2894 >                }
2895 >                else if ((ph = p.hash) > h)
2896 >                    dir = -1;
2897 >                else if (ph < h)
2898 >                    dir = 1;
2899 >                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2900 >                    return p;
2901 >                else if ((kc == null &&
2902 >                          (kc = comparableClassFor(k)) == null) ||
2903 >                         (dir = compareComparables(kc, k, pk)) == 0) {
2904 >                    if (!searched) {
2905 >                        TreeNode<K,V> q, ch;
2906 >                        searched = true;
2907 >                        if (((ch = p.left) != null &&
2908 >                             (q = ch.findTreeNode(h, k, kc)) != null) ||
2909 >                            ((ch = p.right) != null &&
2910 >                             (q = ch.findTreeNode(h, k, kc)) != null))
2911 >                            return q;
2912 >                    }
2913 >                    dir = tieBreakOrder(k, pk);
2914 >                }
2915 >
2916 >                TreeNode<K,V> xp = p;
2917 >                if ((p = (dir <= 0) ? p.left : p.right) == null) {
2918 >                    TreeNode<K,V> x, f = first;
2919 >                    first = x = new TreeNode<K,V>(h, k, v, f, xp);
2920 >                    if (f != null)
2921 >                        f.prev = x;
2922 >                    if (dir <= 0)
2923 >                        xp.left = x;
2924 >                    else
2925 >                        xp.right = x;
2926 >                    if (!xp.red)
2927 >                        x.red = true;
2928 >                    else {
2929 >                        lockRoot();
2930 >                        try {
2931 >                            root = balanceInsertion(root, x);
2932 >                        } finally {
2933 >                            unlockRoot();
2934 >                        }
2935 >                    }
2936 >                    break;
2937 >                }
2938 >            }
2939 >            assert checkInvariants(root);
2940 >            return null;
2941 >        }
2942 >
2943 >        /**
2944 >         * Removes the given node, that must be present before this
2945 >         * call.  This is messier than typical red-black deletion code
2946 >         * because we cannot swap the contents of an interior node
2947 >         * with a leaf successor that is pinned by "next" pointers
2948 >         * that are accessible independently of lock. So instead we
2949 >         * swap the tree linkages.
2950 >         *
2951 >         * @return true if now too small, so should be untreeified
2952 >         */
2953 >        final boolean removeTreeNode(TreeNode<K,V> p) {
2954 >            TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2955 >            TreeNode<K,V> pred = p.prev;  // unlink traversal pointers
2956 >            TreeNode<K,V> r, rl;
2957 >            if (pred == null)
2958 >                first = next;
2959 >            else
2960 >                pred.next = next;
2961 >            if (next != null)
2962 >                next.prev = pred;
2963 >            if (first == null) {
2964 >                root = null;
2965 >                return true;
2966 >            }
2967 >            if ((r = root) == null || r.right == null || // too small
2968 >                (rl = r.left) == null || rl.left == null)
2969 >                return true;
2970 >            lockRoot();
2971 >            try {
2972 >                TreeNode<K,V> replacement;
2973 >                TreeNode<K,V> pl = p.left;
2974 >                TreeNode<K,V> pr = p.right;
2975 >                if (pl != null && pr != null) {
2976 >                    TreeNode<K,V> s = pr, sl;
2977 >                    while ((sl = s.left) != null) // find successor
2978 >                        s = sl;
2979 >                    boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2980 >                    TreeNode<K,V> sr = s.right;
2981 >                    TreeNode<K,V> pp = p.parent;
2982 >                    if (s == pr) { // p was s's direct parent
2983 >                        p.parent = s;
2984 >                        s.right = p;
2985 >                    }
2986 >                    else {
2987 >                        TreeNode<K,V> sp = s.parent;
2988 >                        if ((p.parent = sp) != null) {
2989 >                            if (s == sp.left)
2990 >                                sp.left = p;
2991 >                            else
2992 >                                sp.right = p;
2993 >                        }
2994 >                        if ((s.right = pr) != null)
2995 >                            pr.parent = s;
2996 >                    }
2997 >                    p.left = null;
2998 >                    if ((p.right = sr) != null)
2999 >                        sr.parent = p;
3000 >                    if ((s.left = pl) != null)
3001 >                        pl.parent = s;
3002 >                    if ((s.parent = pp) == null)
3003 >                        r = s;
3004 >                    else if (p == pp.left)
3005 >                        pp.left = s;
3006 >                    else
3007 >                        pp.right = s;
3008 >                    if (sr != null)
3009 >                        replacement = sr;
3010 >                    else
3011 >                        replacement = p;
3012 >                }
3013 >                else if (pl != null)
3014 >                    replacement = pl;
3015 >                else if (pr != null)
3016 >                    replacement = pr;
3017 >                else
3018 >                    replacement = p;
3019 >                if (replacement != p) {
3020 >                    TreeNode<K,V> pp = replacement.parent = p.parent;
3021 >                    if (pp == null)
3022 >                        r = replacement;
3023 >                    else if (p == pp.left)
3024 >                        pp.left = replacement;
3025 >                    else
3026 >                        pp.right = replacement;
3027 >                    p.left = p.right = p.parent = null;
3028 >                }
3029 >
3030 >                root = (p.red) ? r : balanceDeletion(r, replacement);
3031 >
3032 >                if (p == replacement) {  // detach pointers
3033 >                    TreeNode<K,V> pp;
3034 >                    if ((pp = p.parent) != null) {
3035 >                        if (p == pp.left)
3036 >                            pp.left = null;
3037 >                        else if (p == pp.right)
3038 >                            pp.right = null;
3039 >                        p.parent = null;
3040 >                    }
3041 >                }
3042 >            } finally {
3043 >                unlockRoot();
3044 >            }
3045 >            assert checkInvariants(root);
3046 >            return false;
3047 >        }
3048 >
3049 >        /* ------------------------------------------------------------ */
3050 >        // Red-black tree methods, all adapted from CLR
3051 >
3052 >        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
3053 >                                              TreeNode<K,V> p) {
3054 >            TreeNode<K,V> r, pp, rl;
3055 >            if (p != null && (r = p.right) != null) {
3056 >                if ((rl = p.right = r.left) != null)
3057 >                    rl.parent = p;
3058 >                if ((pp = r.parent = p.parent) == null)
3059 >                    (root = r).red = false;
3060 >                else if (pp.left == p)
3061 >                    pp.left = r;
3062 >                else
3063 >                    pp.right = r;
3064 >                r.left = p;
3065 >                p.parent = r;
3066 >            }
3067 >            return root;
3068 >        }
3069 >
3070 >        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
3071 >                                               TreeNode<K,V> p) {
3072 >            TreeNode<K,V> l, pp, lr;
3073 >            if (p != null && (l = p.left) != null) {
3074 >                if ((lr = p.left = l.right) != null)
3075 >                    lr.parent = p;
3076 >                if ((pp = l.parent = p.parent) == null)
3077 >                    (root = l).red = false;
3078 >                else if (pp.right == p)
3079 >                    pp.right = l;
3080 >                else
3081 >                    pp.left = l;
3082 >                l.right = p;
3083 >                p.parent = l;
3084 >            }
3085 >            return root;
3086 >        }
3087 >
3088 >        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
3089 >                                                    TreeNode<K,V> x) {
3090 >            x.red = true;
3091 >            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
3092 >                if ((xp = x.parent) == null) {
3093 >                    x.red = false;
3094 >                    return x;
3095 >                }
3096 >                else if (!xp.red || (xpp = xp.parent) == null)
3097 >                    return root;
3098 >                if (xp == (xppl = xpp.left)) {
3099 >                    if ((xppr = xpp.right) != null && xppr.red) {
3100 >                        xppr.red = false;
3101 >                        xp.red = false;
3102 >                        xpp.red = true;
3103 >                        x = xpp;
3104 >                    }
3105 >                    else {
3106 >                        if (x == xp.right) {
3107 >                            root = rotateLeft(root, x = xp);
3108 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3109 >                        }
3110 >                        if (xp != null) {
3111 >                            xp.red = false;
3112 >                            if (xpp != null) {
3113 >                                xpp.red = true;
3114 >                                root = rotateRight(root, xpp);
3115 >                            }
3116 >                        }
3117 >                    }
3118 >                }
3119 >                else {
3120 >                    if (xppl != null && xppl.red) {
3121 >                        xppl.red = false;
3122 >                        xp.red = false;
3123 >                        xpp.red = true;
3124 >                        x = xpp;
3125 >                    }
3126 >                    else {
3127 >                        if (x == xp.left) {
3128 >                            root = rotateRight(root, x = xp);
3129 >                            xpp = (xp = x.parent) == null ? null : xp.parent;
3130 >                        }
3131 >                        if (xp != null) {
3132 >                            xp.red = false;
3133 >                            if (xpp != null) {
3134 >                                xpp.red = true;
3135 >                                root = rotateLeft(root, xpp);
3136 >                            }
3137 >                        }
3138 >                    }
3139 >                }
3140 >            }
3141 >        }
3142 >
3143 >        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3144 >                                                   TreeNode<K,V> x) {
3145 >            for (TreeNode<K,V> xp, xpl, xpr;;) {
3146 >                if (x == null || x == root)
3147 >                    return root;
3148 >                else if ((xp = x.parent) == null) {
3149 >                    x.red = false;
3150 >                    return x;
3151 >                }
3152 >                else if (x.red) {
3153 >                    x.red = false;
3154 >                    return root;
3155 >                }
3156 >                else if ((xpl = xp.left) == x) {
3157 >                    if ((xpr = xp.right) != null && xpr.red) {
3158 >                        xpr.red = false;
3159 >                        xp.red = true;
3160 >                        root = rotateLeft(root, xp);
3161 >                        xpr = (xp = x.parent) == null ? null : xp.right;
3162 >                    }
3163 >                    if (xpr == null)
3164 >                        x = xp;
3165 >                    else {
3166 >                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3167 >                        if ((sr == null || !sr.red) &&
3168 >                            (sl == null || !sl.red)) {
3169 >                            xpr.red = true;
3170 >                            x = xp;
3171 >                        }
3172 >                        else {
3173 >                            if (sr == null || !sr.red) {
3174 >                                if (sl != null)
3175 >                                    sl.red = false;
3176 >                                xpr.red = true;
3177 >                                root = rotateRight(root, xpr);
3178 >                                xpr = (xp = x.parent) == null ?
3179 >                                    null : xp.right;
3180 >                            }
3181 >                            if (xpr != null) {
3182 >                                xpr.red = (xp == null) ? false : xp.red;
3183 >                                if ((sr = xpr.right) != null)
3184 >                                    sr.red = false;
3185 >                            }
3186 >                            if (xp != null) {
3187 >                                xp.red = false;
3188 >                                root = rotateLeft(root, xp);
3189 >                            }
3190 >                            x = root;
3191 >                        }
3192 >                    }
3193 >                }
3194 >                else { // symmetric
3195 >                    if (xpl != null && xpl.red) {
3196 >                        xpl.red = false;
3197 >                        xp.red = true;
3198 >                        root = rotateRight(root, xp);
3199 >                        xpl = (xp = x.parent) == null ? null : xp.left;
3200 >                    }
3201 >                    if (xpl == null)
3202 >                        x = xp;
3203 >                    else {
3204 >                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3205 >                        if ((sl == null || !sl.red) &&
3206 >                            (sr == null || !sr.red)) {
3207 >                            xpl.red = true;
3208 >                            x = xp;
3209 >                        }
3210 >                        else {
3211 >                            if (sl == null || !sl.red) {
3212 >                                if (sr != null)
3213 >                                    sr.red = false;
3214 >                                xpl.red = true;
3215 >                                root = rotateLeft(root, xpl);
3216 >                                xpl = (xp = x.parent) == null ?
3217 >                                    null : xp.left;
3218 >                            }
3219 >                            if (xpl != null) {
3220 >                                xpl.red = (xp == null) ? false : xp.red;
3221 >                                if ((sl = xpl.left) != null)
3222 >                                    sl.red = false;
3223 >                            }
3224 >                            if (xp != null) {
3225 >                                xp.red = false;
3226 >                                root = rotateRight(root, xp);
3227 >                            }
3228 >                            x = root;
3229 >                        }
3230 >                    }
3231 >                }
3232 >            }
3233 >        }
3234 >
3235 >        /**
3236 >         * Checks invariants recursively for the tree of Nodes rooted at t.
3237 >         */
3238 >        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3239 >            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3240 >                tb = t.prev, tn = (TreeNode<K,V>)t.next;
3241 >            if (tb != null && tb.next != t)
3242 >                return false;
3243 >            if (tn != null && tn.prev != t)
3244 >                return false;
3245 >            if (tp != null && t != tp.left && t != tp.right)
3246 >                return false;
3247 >            if (tl != null && (tl.parent != t || tl.hash > t.hash))
3248 >                return false;
3249 >            if (tr != null && (tr.parent != t || tr.hash < t.hash))
3250 >                return false;
3251 >            if (t.red && tl != null && tl.red && tr != null && tr.red)
3252 >                return false;
3253 >            if (tl != null && !checkInvariants(tl))
3254 >                return false;
3255 >            if (tr != null && !checkInvariants(tr))
3256 >                return false;
3257 >            return true;
3258 >        }
3259 >
3260 >        private static final long LOCKSTATE
3261 >            = U.objectFieldOffset(TreeBin.class, "lockState");
3262 >    }
3263 >
3264 >    /* ----------------Table Traversal -------------- */
3265 >
3266 >    /**
3267 >     * Records the table, its length, and current traversal index for a
3268 >     * traverser that must process a region of a forwarded table before
3269 >     * proceeding with current table.
3270 >     */
3271 >    static final class TableStack<K,V> {
3272 >        int length;
3273 >        int index;
3274 >        Node<K,V>[] tab;
3275 >        TableStack<K,V> next;
3276 >    }
3277 >
3278 >    /**
3279 >     * Encapsulates traversal for methods such as containsValue; also
3280 >     * serves as a base class for other iterators and spliterators.
3281 >     *
3282 >     * Method advance visits once each still-valid node that was
3283 >     * reachable upon iterator construction. It might miss some that
3284 >     * were added to a bin after the bin was visited, which is OK wrt
3285 >     * consistency guarantees. Maintaining this property in the face
3286 >     * of possible ongoing resizes requires a fair amount of
3287 >     * bookkeeping state that is difficult to optimize away amidst
3288 >     * volatile accesses.  Even so, traversal maintains reasonable
3289 >     * throughput.
3290 >     *
3291 >     * Normally, iteration proceeds bin-by-bin traversing lists.
3292 >     * However, if the table has been resized, then all future steps
3293 >     * must traverse both the bin at the current index as well as at
3294 >     * (index + baseSize); and so on for further resizings. To
3295 >     * paranoically cope with potential sharing by users of iterators
3296 >     * across threads, iteration terminates if a bounds checks fails
3297 >     * for a table read.
3298 >     */
3299 >    static class Traverser<K,V> {
3300 >        Node<K,V>[] tab;        // current table; updated if resized
3301 >        Node<K,V> next;         // the next entry to use
3302 >        TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3303 >        int index;              // index of bin to use next
3304 >        int baseIndex;          // current index of initial table
3305 >        int baseLimit;          // index bound for initial table
3306 >        final int baseSize;     // initial table size
3307 >
3308 >        Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3309 >            this.tab = tab;
3310 >            this.baseSize = size;
3311 >            this.baseIndex = this.index = index;
3312 >            this.baseLimit = limit;
3313 >            this.next = null;
3314 >        }
3315 >
3316 >        /**
3317 >         * Advances if possible, returning next valid node, or null if none.
3318 >         */
3319 >        final Node<K,V> advance() {
3320 >            Node<K,V> e;
3321 >            if ((e = next) != null)
3322 >                e = e.next;
3323 >            for (;;) {
3324 >                Node<K,V>[] t; int i, n;  // must use locals in checks
3325 >                if (e != null)
3326 >                    return next = e;
3327 >                if (baseIndex >= baseLimit || (t = tab) == null ||
3328 >                    (n = t.length) <= (i = index) || i < 0)
3329 >                    return next = null;
3330 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
3331 >                    if (e instanceof ForwardingNode) {
3332 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
3333 >                        e = null;
3334 >                        pushState(t, i, n);
3335 >                        continue;
3336 >                    }
3337 >                    else if (e instanceof TreeBin)
3338 >                        e = ((TreeBin<K,V>)e).first;
3339 >                    else
3340 >                        e = null;
3341 >                }
3342 >                if (stack != null)
3343 >                    recoverState(n);
3344 >                else if ((index = i + baseSize) >= n)
3345 >                    index = ++baseIndex; // visit upper slots if present
3346 >            }
3347 >        }
3348 >
3349 >        /**
3350 >         * Saves traversal state upon encountering a forwarding node.
3351 >         */
3352 >        private void pushState(Node<K,V>[] t, int i, int n) {
3353 >            TableStack<K,V> s = spare;  // reuse if possible
3354 >            if (s != null)
3355 >                spare = s.next;
3356 >            else
3357 >                s = new TableStack<K,V>();
3358 >            s.tab = t;
3359 >            s.length = n;
3360 >            s.index = i;
3361 >            s.next = stack;
3362 >            stack = s;
3363 >        }
3364 >
3365 >        /**
3366 >         * Possibly pops traversal state.
3367 >         *
3368 >         * @param n length of current table
3369 >         */
3370 >        private void recoverState(int n) {
3371 >            TableStack<K,V> s; int len;
3372 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
3373 >                n = len;
3374 >                index = s.index;
3375 >                tab = s.tab;
3376 >                s.tab = null;
3377 >                TableStack<K,V> next = s.next;
3378 >                s.next = spare; // save for reuse
3379 >                stack = next;
3380 >                spare = s;
3381 >            }
3382 >            if (s == null && (index += baseSize) >= n)
3383 >                index = ++baseIndex;
3384 >        }
3385 >    }
3386 >
3387 >    /**
3388 >     * Base of key, value, and entry Iterators. Adds fields to
3389 >     * Traverser to support iterator.remove.
3390 >     */
3391 >    static class BaseIterator<K,V> extends Traverser<K,V> {
3392 >        final ConcurrentHashMap<K,V> map;
3393 >        Node<K,V> lastReturned;
3394 >        BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3395 >                    ConcurrentHashMap<K,V> map) {
3396 >            super(tab, size, index, limit);
3397 >            this.map = map;
3398              advance();
1101            return lastReturned;
3399          }
3400  
3401 <        public void remove() {
3402 <            if (lastReturned == null)
3401 >        public final boolean hasNext() { return next != null; }
3402 >        public final boolean hasMoreElements() { return next != null; }
3403 >
3404 >        public final void remove() {
3405 >            Node<K,V> p;
3406 >            if ((p = lastReturned) == null)
3407                  throw new IllegalStateException();
1107            ConcurrentHashMap.this.remove(lastReturned.key);
3408              lastReturned = null;
3409 +            map.replaceNode(p.key, null, null);
3410 +        }
3411 +    }
3412 +
3413 +    static final class KeyIterator<K,V> extends BaseIterator<K,V>
3414 +        implements Iterator<K>, Enumeration<K> {
3415 +        KeyIterator(Node<K,V>[] tab, int size, int index, int limit,
3416 +                    ConcurrentHashMap<K,V> map) {
3417 +            super(tab, size, index, limit, map);
3418 +        }
3419 +
3420 +        public final K next() {
3421 +            Node<K,V> p;
3422 +            if ((p = next) == null)
3423 +                throw new NoSuchElementException();
3424 +            K k = p.key;
3425 +            lastReturned = p;
3426 +            advance();
3427 +            return k;
3428          }
3429 +
3430 +        public final K nextElement() { return next(); }
3431      }
3432  
3433 <    final class KeyIterator
3434 <        extends HashIterator
3435 <        implements Iterator<K>, Enumeration<K>
3436 <    {
3437 <        public K next()        { return super.nextEntry().key; }
3438 <        public K nextElement() { return super.nextEntry().key; }
3433 >    static final class ValueIterator<K,V> extends BaseIterator<K,V>
3434 >        implements Iterator<V>, Enumeration<V> {
3435 >        ValueIterator(Node<K,V>[] tab, int size, int index, int limit,
3436 >                      ConcurrentHashMap<K,V> map) {
3437 >            super(tab, size, index, limit, map);
3438 >        }
3439 >
3440 >        public final V next() {
3441 >            Node<K,V> p;
3442 >            if ((p = next) == null)
3443 >                throw new NoSuchElementException();
3444 >            V v = p.val;
3445 >            lastReturned = p;
3446 >            advance();
3447 >            return v;
3448 >        }
3449 >
3450 >        public final V nextElement() { return next(); }
3451      }
3452  
3453 <    final class ValueIterator
3454 <        extends HashIterator
3455 <        implements Iterator<V>, Enumeration<V>
3456 <    {
3457 <        public V next()        { return super.nextEntry().value; }
3458 <        public V nextElement() { return super.nextEntry().value; }
3453 >    static final class EntryIterator<K,V> extends BaseIterator<K,V>
3454 >        implements Iterator<Map.Entry<K,V>> {
3455 >        EntryIterator(Node<K,V>[] tab, int size, int index, int limit,
3456 >                      ConcurrentHashMap<K,V> map) {
3457 >            super(tab, size, index, limit, map);
3458 >        }
3459 >
3460 >        public final Map.Entry<K,V> next() {
3461 >            Node<K,V> p;
3462 >            if ((p = next) == null)
3463 >                throw new NoSuchElementException();
3464 >            K k = p.key;
3465 >            V v = p.val;
3466 >            lastReturned = p;
3467 >            advance();
3468 >            return new MapEntry<K,V>(k, v, map);
3469 >        }
3470      }
3471  
3472      /**
3473 <     * Custom Entry class used by EntryIterator.next(), that relays
1130 <     * setValue changes to the underlying map.
3473 >     * Exported Entry for EntryIterator.
3474       */
3475 <    final class WriteThroughEntry
3476 <        extends AbstractMap.SimpleEntry<K,V>
3477 <    {
3478 <        WriteThroughEntry(K k, V v) {
3479 <            super(k,v);
3475 >    static final class MapEntry<K,V> implements Map.Entry<K,V> {
3476 >        final K key; // non-null
3477 >        V val;       // non-null
3478 >        final ConcurrentHashMap<K,V> map;
3479 >        MapEntry(K key, V val, ConcurrentHashMap<K,V> map) {
3480 >            this.key = key;
3481 >            this.val = val;
3482 >            this.map = map;
3483 >        }
3484 >        public K getKey()        { return key; }
3485 >        public V getValue()      { return val; }
3486 >        public int hashCode()    { return key.hashCode() ^ val.hashCode(); }
3487 >        public String toString() {
3488 >            return Helpers.mapEntryToString(key, val);
3489 >        }
3490 >
3491 >        public boolean equals(Object o) {
3492 >            Object k, v; Map.Entry<?,?> e;
3493 >            return ((o instanceof Map.Entry) &&
3494 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3495 >                    (v = e.getValue()) != null &&
3496 >                    (k == key || k.equals(key)) &&
3497 >                    (v == val || v.equals(val)));
3498          }
3499  
3500          /**
3501 <         * Set our entry's value and write through to the map. The
3502 <         * value to return is somewhat arbitrary here. Since a
3503 <         * WriteThroughEntry does not necessarily track asynchronous
3504 <         * changes, the most recent "previous" value could be
3505 <         * different from what we return (or could even have been
3506 <         * removed in which case the put will re-establish). We do not
1146 <         * and cannot guarantee more.
3501 >         * Sets our entry's value and writes through to the map. The
3502 >         * value to return is somewhat arbitrary here. Since we do not
3503 >         * necessarily track asynchronous changes, the most recent
3504 >         * "previous" value could be different from what we return (or
3505 >         * could even have been removed, in which case the put will
3506 >         * re-establish). We do not and cannot guarantee more.
3507           */
3508 <        public V setValue(V value) {
3508 >        public V setValue(V value) {
3509              if (value == null) throw new NullPointerException();
3510 <            V v = super.setValue(value);
3511 <            ConcurrentHashMap.this.put(getKey(), value);
3510 >            V v = val;
3511 >            val = value;
3512 >            map.put(key, value);
3513              return v;
3514          }
3515      }
3516  
3517 <    final class EntryIterator
3518 <        extends HashIterator
3519 <        implements Iterator<Entry<K,V>>
3520 <    {
3521 <        public Map.Entry<K,V> next() {
3522 <            HashEntry<K,V> e = super.nextEntry();
3523 <            return new WriteThroughEntry(e.key, e.value);
3517 >    static final class KeySpliterator<K,V> extends Traverser<K,V>
3518 >        implements Spliterator<K> {
3519 >        long est;               // size estimate
3520 >        KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3521 >                       long est) {
3522 >            super(tab, size, index, limit);
3523 >            this.est = est;
3524 >        }
3525 >
3526 >        public KeySpliterator<K,V> trySplit() {
3527 >            int i, f, h;
3528 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3529 >                new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3530 >                                        f, est >>>= 1);
3531 >        }
3532 >
3533 >        public void forEachRemaining(Consumer<? super K> action) {
3534 >            if (action == null) throw new NullPointerException();
3535 >            for (Node<K,V> p; (p = advance()) != null;)
3536 >                action.accept(p.key);
3537 >        }
3538 >
3539 >        public boolean tryAdvance(Consumer<? super K> action) {
3540 >            if (action == null) throw new NullPointerException();
3541 >            Node<K,V> p;
3542 >            if ((p = advance()) == null)
3543 >                return false;
3544 >            action.accept(p.key);
3545 >            return true;
3546 >        }
3547 >
3548 >        public long estimateSize() { return est; }
3549 >
3550 >        public int characteristics() {
3551 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3552 >                Spliterator.NONNULL;
3553          }
3554      }
3555  
3556 <    final class KeySet extends AbstractSet<K> {
3557 <        public Iterator<K> iterator() {
3558 <            return new KeyIterator();
3556 >    static final class ValueSpliterator<K,V> extends Traverser<K,V>
3557 >        implements Spliterator<V> {
3558 >        long est;               // size estimate
3559 >        ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3560 >                         long est) {
3561 >            super(tab, size, index, limit);
3562 >            this.est = est;
3563 >        }
3564 >
3565 >        public ValueSpliterator<K,V> trySplit() {
3566 >            int i, f, h;
3567 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3568 >                new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3569 >                                          f, est >>>= 1);
3570 >        }
3571 >
3572 >        public void forEachRemaining(Consumer<? super V> action) {
3573 >            if (action == null) throw new NullPointerException();
3574 >            for (Node<K,V> p; (p = advance()) != null;)
3575 >                action.accept(p.val);
3576 >        }
3577 >
3578 >        public boolean tryAdvance(Consumer<? super V> action) {
3579 >            if (action == null) throw new NullPointerException();
3580 >            Node<K,V> p;
3581 >            if ((p = advance()) == null)
3582 >                return false;
3583 >            action.accept(p.val);
3584 >            return true;
3585          }
3586 <        public int size() {
3587 <            return ConcurrentHashMap.this.size();
3586 >
3587 >        public long estimateSize() { return est; }
3588 >
3589 >        public int characteristics() {
3590 >            return Spliterator.CONCURRENT | Spliterator.NONNULL;
3591          }
3592 <        public boolean contains(Object o) {
3593 <            return ConcurrentHashMap.this.containsKey(o);
3592 >    }
3593 >
3594 >    static final class EntrySpliterator<K,V> extends Traverser<K,V>
3595 >        implements Spliterator<Map.Entry<K,V>> {
3596 >        final ConcurrentHashMap<K,V> map; // To export MapEntry
3597 >        long est;               // size estimate
3598 >        EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3599 >                         long est, ConcurrentHashMap<K,V> map) {
3600 >            super(tab, size, index, limit);
3601 >            this.map = map;
3602 >            this.est = est;
3603 >        }
3604 >
3605 >        public EntrySpliterator<K,V> trySplit() {
3606 >            int i, f, h;
3607 >            return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3608 >                new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3609 >                                          f, est >>>= 1, map);
3610 >        }
3611 >
3612 >        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
3613 >            if (action == null) throw new NullPointerException();
3614 >            for (Node<K,V> p; (p = advance()) != null; )
3615 >                action.accept(new MapEntry<K,V>(p.key, p.val, map));
3616 >        }
3617 >
3618 >        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
3619 >            if (action == null) throw new NullPointerException();
3620 >            Node<K,V> p;
3621 >            if ((p = advance()) == null)
3622 >                return false;
3623 >            action.accept(new MapEntry<K,V>(p.key, p.val, map));
3624 >            return true;
3625          }
3626 <        public boolean remove(Object o) {
3627 <            return ConcurrentHashMap.this.remove(o) != null;
3626 >
3627 >        public long estimateSize() { return est; }
3628 >
3629 >        public int characteristics() {
3630 >            return Spliterator.DISTINCT | Spliterator.CONCURRENT |
3631 >                Spliterator.NONNULL;
3632          }
3633 <        public void clear() {
3634 <            ConcurrentHashMap.this.clear();
3633 >    }
3634 >
3635 >    // Parallel bulk operations
3636 >
3637 >    /**
3638 >     * Computes initial batch value for bulk tasks. The returned value
3639 >     * is approximately exp2 of the number of times (minus one) to
3640 >     * split task by two before executing leaf action. This value is
3641 >     * faster to compute and more convenient to use as a guide to
3642 >     * splitting than is the depth, since it is used while dividing by
3643 >     * two anyway.
3644 >     */
3645 >    final int batchFor(long b) {
3646 >        long n;
3647 >        if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3648 >            return 0;
3649 >        int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3650 >        return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3651 >    }
3652 >
3653 >    /**
3654 >     * Performs the given action for each (key, value).
3655 >     *
3656 >     * @param parallelismThreshold the (estimated) number of elements
3657 >     * needed for this operation to be executed in parallel
3658 >     * @param action the action
3659 >     * @since 1.8
3660 >     */
3661 >    public void forEach(long parallelismThreshold,
3662 >                        BiConsumer<? super K,? super V> action) {
3663 >        if (action == null) throw new NullPointerException();
3664 >        new ForEachMappingTask<K,V>
3665 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3666 >             action).invoke();
3667 >    }
3668 >
3669 >    /**
3670 >     * Performs the given action for each non-null transformation
3671 >     * of each (key, value).
3672 >     *
3673 >     * @param parallelismThreshold the (estimated) number of elements
3674 >     * needed for this operation to be executed in parallel
3675 >     * @param transformer a function returning the transformation
3676 >     * for an element, or null if there is no transformation (in
3677 >     * which case the action is not applied)
3678 >     * @param action the action
3679 >     * @param <U> the return type of the transformer
3680 >     * @since 1.8
3681 >     */
3682 >    public <U> void forEach(long parallelismThreshold,
3683 >                            BiFunction<? super K, ? super V, ? extends U> transformer,
3684 >                            Consumer<? super U> action) {
3685 >        if (transformer == null || action == null)
3686 >            throw new NullPointerException();
3687 >        new ForEachTransformedMappingTask<K,V,U>
3688 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3689 >             transformer, action).invoke();
3690 >    }
3691 >
3692 >    /**
3693 >     * Returns a non-null result from applying the given search
3694 >     * function on each (key, value), or null if none.  Upon
3695 >     * success, further element processing is suppressed and the
3696 >     * results of any other parallel invocations of the search
3697 >     * function are ignored.
3698 >     *
3699 >     * @param parallelismThreshold the (estimated) number of elements
3700 >     * needed for this operation to be executed in parallel
3701 >     * @param searchFunction a function returning a non-null
3702 >     * result on success, else null
3703 >     * @param <U> the return type of the search function
3704 >     * @return a non-null result from applying the given search
3705 >     * function on each (key, value), or null if none
3706 >     * @since 1.8
3707 >     */
3708 >    public <U> U search(long parallelismThreshold,
3709 >                        BiFunction<? super K, ? super V, ? extends U> searchFunction) {
3710 >        if (searchFunction == null) throw new NullPointerException();
3711 >        return new SearchMappingsTask<K,V,U>
3712 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3713 >             searchFunction, new AtomicReference<U>()).invoke();
3714 >    }
3715 >
3716 >    /**
3717 >     * Returns the result of accumulating the given transformation
3718 >     * of all (key, value) pairs using the given reducer to
3719 >     * combine values, or null if none.
3720 >     *
3721 >     * @param parallelismThreshold the (estimated) number of elements
3722 >     * needed for this operation to be executed in parallel
3723 >     * @param transformer a function returning the transformation
3724 >     * for an element, or null if there is no transformation (in
3725 >     * which case it is not combined)
3726 >     * @param reducer a commutative associative combining function
3727 >     * @param <U> the return type of the transformer
3728 >     * @return the result of accumulating the given transformation
3729 >     * of all (key, value) pairs
3730 >     * @since 1.8
3731 >     */
3732 >    public <U> U reduce(long parallelismThreshold,
3733 >                        BiFunction<? super K, ? super V, ? extends U> transformer,
3734 >                        BiFunction<? super U, ? super U, ? extends U> reducer) {
3735 >        if (transformer == null || reducer == null)
3736 >            throw new NullPointerException();
3737 >        return new MapReduceMappingsTask<K,V,U>
3738 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3739 >             null, transformer, reducer).invoke();
3740 >    }
3741 >
3742 >    /**
3743 >     * Returns the result of accumulating the given transformation
3744 >     * of all (key, value) pairs using the given reducer to
3745 >     * combine values, and the given basis as an identity value.
3746 >     *
3747 >     * @param parallelismThreshold the (estimated) number of elements
3748 >     * needed for this operation to be executed in parallel
3749 >     * @param transformer a function returning the transformation
3750 >     * for an element
3751 >     * @param basis the identity (initial default value) for the reduction
3752 >     * @param reducer a commutative associative combining function
3753 >     * @return the result of accumulating the given transformation
3754 >     * of all (key, value) pairs
3755 >     * @since 1.8
3756 >     */
3757 >    public double reduceToDouble(long parallelismThreshold,
3758 >                                 ToDoubleBiFunction<? super K, ? super V> transformer,
3759 >                                 double basis,
3760 >                                 DoubleBinaryOperator reducer) {
3761 >        if (transformer == null || reducer == null)
3762 >            throw new NullPointerException();
3763 >        return new MapReduceMappingsToDoubleTask<K,V>
3764 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3765 >             null, transformer, basis, reducer).invoke();
3766 >    }
3767 >
3768 >    /**
3769 >     * Returns the result of accumulating the given transformation
3770 >     * of all (key, value) pairs using the given reducer to
3771 >     * combine values, and the given basis as an identity value.
3772 >     *
3773 >     * @param parallelismThreshold the (estimated) number of elements
3774 >     * needed for this operation to be executed in parallel
3775 >     * @param transformer a function returning the transformation
3776 >     * for an element
3777 >     * @param basis the identity (initial default value) for the reduction
3778 >     * @param reducer a commutative associative combining function
3779 >     * @return the result of accumulating the given transformation
3780 >     * of all (key, value) pairs
3781 >     * @since 1.8
3782 >     */
3783 >    public long reduceToLong(long parallelismThreshold,
3784 >                             ToLongBiFunction<? super K, ? super V> transformer,
3785 >                             long basis,
3786 >                             LongBinaryOperator reducer) {
3787 >        if (transformer == null || reducer == null)
3788 >            throw new NullPointerException();
3789 >        return new MapReduceMappingsToLongTask<K,V>
3790 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3791 >             null, transformer, basis, reducer).invoke();
3792 >    }
3793 >
3794 >    /**
3795 >     * Returns the result of accumulating the given transformation
3796 >     * of all (key, value) pairs using the given reducer to
3797 >     * combine values, and the given basis as an identity value.
3798 >     *
3799 >     * @param parallelismThreshold the (estimated) number of elements
3800 >     * needed for this operation to be executed in parallel
3801 >     * @param transformer a function returning the transformation
3802 >     * for an element
3803 >     * @param basis the identity (initial default value) for the reduction
3804 >     * @param reducer a commutative associative combining function
3805 >     * @return the result of accumulating the given transformation
3806 >     * of all (key, value) pairs
3807 >     * @since 1.8
3808 >     */
3809 >    public int reduceToInt(long parallelismThreshold,
3810 >                           ToIntBiFunction<? super K, ? super V> transformer,
3811 >                           int basis,
3812 >                           IntBinaryOperator reducer) {
3813 >        if (transformer == null || reducer == null)
3814 >            throw new NullPointerException();
3815 >        return new MapReduceMappingsToIntTask<K,V>
3816 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3817 >             null, transformer, basis, reducer).invoke();
3818 >    }
3819 >
3820 >    /**
3821 >     * Performs the given action for each key.
3822 >     *
3823 >     * @param parallelismThreshold the (estimated) number of elements
3824 >     * needed for this operation to be executed in parallel
3825 >     * @param action the action
3826 >     * @since 1.8
3827 >     */
3828 >    public void forEachKey(long parallelismThreshold,
3829 >                           Consumer<? super K> action) {
3830 >        if (action == null) throw new NullPointerException();
3831 >        new ForEachKeyTask<K,V>
3832 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3833 >             action).invoke();
3834 >    }
3835 >
3836 >    /**
3837 >     * Performs the given action for each non-null transformation
3838 >     * of each key.
3839 >     *
3840 >     * @param parallelismThreshold the (estimated) number of elements
3841 >     * needed for this operation to be executed in parallel
3842 >     * @param transformer a function returning the transformation
3843 >     * for an element, or null if there is no transformation (in
3844 >     * which case the action is not applied)
3845 >     * @param action the action
3846 >     * @param <U> the return type of the transformer
3847 >     * @since 1.8
3848 >     */
3849 >    public <U> void forEachKey(long parallelismThreshold,
3850 >                               Function<? super K, ? extends U> transformer,
3851 >                               Consumer<? super U> action) {
3852 >        if (transformer == null || action == null)
3853 >            throw new NullPointerException();
3854 >        new ForEachTransformedKeyTask<K,V,U>
3855 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3856 >             transformer, action).invoke();
3857 >    }
3858 >
3859 >    /**
3860 >     * Returns a non-null result from applying the given search
3861 >     * function on each key, or null if none. Upon success,
3862 >     * further element processing is suppressed and the results of
3863 >     * any other parallel invocations of the search function are
3864 >     * ignored.
3865 >     *
3866 >     * @param parallelismThreshold the (estimated) number of elements
3867 >     * needed for this operation to be executed in parallel
3868 >     * @param searchFunction a function returning a non-null
3869 >     * result on success, else null
3870 >     * @param <U> the return type of the search function
3871 >     * @return a non-null result from applying the given search
3872 >     * function on each key, or null if none
3873 >     * @since 1.8
3874 >     */
3875 >    public <U> U searchKeys(long parallelismThreshold,
3876 >                            Function<? super K, ? extends U> searchFunction) {
3877 >        if (searchFunction == null) throw new NullPointerException();
3878 >        return new SearchKeysTask<K,V,U>
3879 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3880 >             searchFunction, new AtomicReference<U>()).invoke();
3881 >    }
3882 >
3883 >    /**
3884 >     * Returns the result of accumulating all keys using the given
3885 >     * reducer to combine values, or null if none.
3886 >     *
3887 >     * @param parallelismThreshold the (estimated) number of elements
3888 >     * needed for this operation to be executed in parallel
3889 >     * @param reducer a commutative associative combining function
3890 >     * @return the result of accumulating all keys using the given
3891 >     * reducer to combine values, or null if none
3892 >     * @since 1.8
3893 >     */
3894 >    public K reduceKeys(long parallelismThreshold,
3895 >                        BiFunction<? super K, ? super K, ? extends K> reducer) {
3896 >        if (reducer == null) throw new NullPointerException();
3897 >        return new ReduceKeysTask<K,V>
3898 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3899 >             null, reducer).invoke();
3900 >    }
3901 >
3902 >    /**
3903 >     * Returns the result of accumulating the given transformation
3904 >     * of all keys using the given reducer to combine values, or
3905 >     * null if none.
3906 >     *
3907 >     * @param parallelismThreshold the (estimated) number of elements
3908 >     * needed for this operation to be executed in parallel
3909 >     * @param transformer a function returning the transformation
3910 >     * for an element, or null if there is no transformation (in
3911 >     * which case it is not combined)
3912 >     * @param reducer a commutative associative combining function
3913 >     * @param <U> the return type of the transformer
3914 >     * @return the result of accumulating the given transformation
3915 >     * of all keys
3916 >     * @since 1.8
3917 >     */
3918 >    public <U> U reduceKeys(long parallelismThreshold,
3919 >                            Function<? super K, ? extends U> transformer,
3920 >         BiFunction<? super U, ? super U, ? extends U> reducer) {
3921 >        if (transformer == null || reducer == null)
3922 >            throw new NullPointerException();
3923 >        return new MapReduceKeysTask<K,V,U>
3924 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3925 >             null, transformer, reducer).invoke();
3926 >    }
3927 >
3928 >    /**
3929 >     * Returns the result of accumulating the given transformation
3930 >     * of all keys using the given reducer to combine values, and
3931 >     * the given basis as an identity value.
3932 >     *
3933 >     * @param parallelismThreshold the (estimated) number of elements
3934 >     * needed for this operation to be executed in parallel
3935 >     * @param transformer a function returning the transformation
3936 >     * for an element
3937 >     * @param basis the identity (initial default value) for the reduction
3938 >     * @param reducer a commutative associative combining function
3939 >     * @return the result of accumulating the given transformation
3940 >     * of all keys
3941 >     * @since 1.8
3942 >     */
3943 >    public double reduceKeysToDouble(long parallelismThreshold,
3944 >                                     ToDoubleFunction<? super K> transformer,
3945 >                                     double basis,
3946 >                                     DoubleBinaryOperator reducer) {
3947 >        if (transformer == null || reducer == null)
3948 >            throw new NullPointerException();
3949 >        return new MapReduceKeysToDoubleTask<K,V>
3950 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3951 >             null, transformer, basis, reducer).invoke();
3952 >    }
3953 >
3954 >    /**
3955 >     * Returns the result of accumulating the given transformation
3956 >     * of all keys using the given reducer to combine values, and
3957 >     * the given basis as an identity value.
3958 >     *
3959 >     * @param parallelismThreshold the (estimated) number of elements
3960 >     * needed for this operation to be executed in parallel
3961 >     * @param transformer a function returning the transformation
3962 >     * for an element
3963 >     * @param basis the identity (initial default value) for the reduction
3964 >     * @param reducer a commutative associative combining function
3965 >     * @return the result of accumulating the given transformation
3966 >     * of all keys
3967 >     * @since 1.8
3968 >     */
3969 >    public long reduceKeysToLong(long parallelismThreshold,
3970 >                                 ToLongFunction<? super K> transformer,
3971 >                                 long basis,
3972 >                                 LongBinaryOperator reducer) {
3973 >        if (transformer == null || reducer == null)
3974 >            throw new NullPointerException();
3975 >        return new MapReduceKeysToLongTask<K,V>
3976 >            (null, batchFor(parallelismThreshold), 0, 0, table,
3977 >             null, transformer, basis, reducer).invoke();
3978 >    }
3979 >
3980 >    /**
3981 >     * Returns the result of accumulating the given transformation
3982 >     * of all keys using the given reducer to combine values, and
3983 >     * the given basis as an identity value.
3984 >     *
3985 >     * @param parallelismThreshold the (estimated) number of elements
3986 >     * needed for this operation to be executed in parallel
3987 >     * @param transformer a function returning the transformation
3988 >     * for an element
3989 >     * @param basis the identity (initial default value) for the reduction
3990 >     * @param reducer a commutative associative combining function
3991 >     * @return the result of accumulating the given transformation
3992 >     * of all keys
3993 >     * @since 1.8
3994 >     */
3995 >    public int reduceKeysToInt(long parallelismThreshold,
3996 >                               ToIntFunction<? super K> transformer,
3997 >                               int basis,
3998 >                               IntBinaryOperator reducer) {
3999 >        if (transformer == null || reducer == null)
4000 >            throw new NullPointerException();
4001 >        return new MapReduceKeysToIntTask<K,V>
4002 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4003 >             null, transformer, basis, reducer).invoke();
4004 >    }
4005 >
4006 >    /**
4007 >     * Performs the given action for each value.
4008 >     *
4009 >     * @param parallelismThreshold the (estimated) number of elements
4010 >     * needed for this operation to be executed in parallel
4011 >     * @param action the action
4012 >     * @since 1.8
4013 >     */
4014 >    public void forEachValue(long parallelismThreshold,
4015 >                             Consumer<? super V> action) {
4016 >        if (action == null)
4017 >            throw new NullPointerException();
4018 >        new ForEachValueTask<K,V>
4019 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4020 >             action).invoke();
4021 >    }
4022 >
4023 >    /**
4024 >     * Performs the given action for each non-null transformation
4025 >     * of each value.
4026 >     *
4027 >     * @param parallelismThreshold the (estimated) number of elements
4028 >     * needed for this operation to be executed in parallel
4029 >     * @param transformer a function returning the transformation
4030 >     * for an element, or null if there is no transformation (in
4031 >     * which case the action is not applied)
4032 >     * @param action the action
4033 >     * @param <U> the return type of the transformer
4034 >     * @since 1.8
4035 >     */
4036 >    public <U> void forEachValue(long parallelismThreshold,
4037 >                                 Function<? super V, ? extends U> transformer,
4038 >                                 Consumer<? super U> action) {
4039 >        if (transformer == null || action == null)
4040 >            throw new NullPointerException();
4041 >        new ForEachTransformedValueTask<K,V,U>
4042 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4043 >             transformer, action).invoke();
4044 >    }
4045 >
4046 >    /**
4047 >     * Returns a non-null result from applying the given search
4048 >     * function on each value, or null if none.  Upon success,
4049 >     * further element processing is suppressed and the results of
4050 >     * any other parallel invocations of the search function are
4051 >     * ignored.
4052 >     *
4053 >     * @param parallelismThreshold the (estimated) number of elements
4054 >     * needed for this operation to be executed in parallel
4055 >     * @param searchFunction a function returning a non-null
4056 >     * result on success, else null
4057 >     * @param <U> the return type of the search function
4058 >     * @return a non-null result from applying the given search
4059 >     * function on each value, or null if none
4060 >     * @since 1.8
4061 >     */
4062 >    public <U> U searchValues(long parallelismThreshold,
4063 >                              Function<? super V, ? extends U> searchFunction) {
4064 >        if (searchFunction == null) throw new NullPointerException();
4065 >        return new SearchValuesTask<K,V,U>
4066 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4067 >             searchFunction, new AtomicReference<U>()).invoke();
4068 >    }
4069 >
4070 >    /**
4071 >     * Returns the result of accumulating all values using the
4072 >     * given reducer to combine values, or null if none.
4073 >     *
4074 >     * @param parallelismThreshold the (estimated) number of elements
4075 >     * needed for this operation to be executed in parallel
4076 >     * @param reducer a commutative associative combining function
4077 >     * @return the result of accumulating all values
4078 >     * @since 1.8
4079 >     */
4080 >    public V reduceValues(long parallelismThreshold,
4081 >                          BiFunction<? super V, ? super V, ? extends V> reducer) {
4082 >        if (reducer == null) throw new NullPointerException();
4083 >        return new ReduceValuesTask<K,V>
4084 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4085 >             null, reducer).invoke();
4086 >    }
4087 >
4088 >    /**
4089 >     * Returns the result of accumulating the given transformation
4090 >     * of all values using the given reducer to combine values, or
4091 >     * null if none.
4092 >     *
4093 >     * @param parallelismThreshold the (estimated) number of elements
4094 >     * needed for this operation to be executed in parallel
4095 >     * @param transformer a function returning the transformation
4096 >     * for an element, or null if there is no transformation (in
4097 >     * which case it is not combined)
4098 >     * @param reducer a commutative associative combining function
4099 >     * @param <U> the return type of the transformer
4100 >     * @return the result of accumulating the given transformation
4101 >     * of all values
4102 >     * @since 1.8
4103 >     */
4104 >    public <U> U reduceValues(long parallelismThreshold,
4105 >                              Function<? super V, ? extends U> transformer,
4106 >                              BiFunction<? super U, ? super U, ? extends U> reducer) {
4107 >        if (transformer == null || reducer == null)
4108 >            throw new NullPointerException();
4109 >        return new MapReduceValuesTask<K,V,U>
4110 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4111 >             null, transformer, reducer).invoke();
4112 >    }
4113 >
4114 >    /**
4115 >     * Returns the result of accumulating the given transformation
4116 >     * of all values using the given reducer to combine values,
4117 >     * and the given basis as an identity value.
4118 >     *
4119 >     * @param parallelismThreshold the (estimated) number of elements
4120 >     * needed for this operation to be executed in parallel
4121 >     * @param transformer a function returning the transformation
4122 >     * for an element
4123 >     * @param basis the identity (initial default value) for the reduction
4124 >     * @param reducer a commutative associative combining function
4125 >     * @return the result of accumulating the given transformation
4126 >     * of all values
4127 >     * @since 1.8
4128 >     */
4129 >    public double reduceValuesToDouble(long parallelismThreshold,
4130 >                                       ToDoubleFunction<? super V> transformer,
4131 >                                       double basis,
4132 >                                       DoubleBinaryOperator reducer) {
4133 >        if (transformer == null || reducer == null)
4134 >            throw new NullPointerException();
4135 >        return new MapReduceValuesToDoubleTask<K,V>
4136 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4137 >             null, transformer, basis, reducer).invoke();
4138 >    }
4139 >
4140 >    /**
4141 >     * Returns the result of accumulating the given transformation
4142 >     * of all values using the given reducer to combine values,
4143 >     * and the given basis as an identity value.
4144 >     *
4145 >     * @param parallelismThreshold the (estimated) number of elements
4146 >     * needed for this operation to be executed in parallel
4147 >     * @param transformer a function returning the transformation
4148 >     * for an element
4149 >     * @param basis the identity (initial default value) for the reduction
4150 >     * @param reducer a commutative associative combining function
4151 >     * @return the result of accumulating the given transformation
4152 >     * of all values
4153 >     * @since 1.8
4154 >     */
4155 >    public long reduceValuesToLong(long parallelismThreshold,
4156 >                                   ToLongFunction<? super V> transformer,
4157 >                                   long basis,
4158 >                                   LongBinaryOperator reducer) {
4159 >        if (transformer == null || reducer == null)
4160 >            throw new NullPointerException();
4161 >        return new MapReduceValuesToLongTask<K,V>
4162 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4163 >             null, transformer, basis, reducer).invoke();
4164 >    }
4165 >
4166 >    /**
4167 >     * Returns the result of accumulating the given transformation
4168 >     * of all values using the given reducer to combine values,
4169 >     * and the given basis as an identity value.
4170 >     *
4171 >     * @param parallelismThreshold the (estimated) number of elements
4172 >     * needed for this operation to be executed in parallel
4173 >     * @param transformer a function returning the transformation
4174 >     * for an element
4175 >     * @param basis the identity (initial default value) for the reduction
4176 >     * @param reducer a commutative associative combining function
4177 >     * @return the result of accumulating the given transformation
4178 >     * of all values
4179 >     * @since 1.8
4180 >     */
4181 >    public int reduceValuesToInt(long parallelismThreshold,
4182 >                                 ToIntFunction<? super V> transformer,
4183 >                                 int basis,
4184 >                                 IntBinaryOperator reducer) {
4185 >        if (transformer == null || reducer == null)
4186 >            throw new NullPointerException();
4187 >        return new MapReduceValuesToIntTask<K,V>
4188 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4189 >             null, transformer, basis, reducer).invoke();
4190 >    }
4191 >
4192 >    /**
4193 >     * Performs the given action for each entry.
4194 >     *
4195 >     * @param parallelismThreshold the (estimated) number of elements
4196 >     * needed for this operation to be executed in parallel
4197 >     * @param action the action
4198 >     * @since 1.8
4199 >     */
4200 >    public void forEachEntry(long parallelismThreshold,
4201 >                             Consumer<? super Map.Entry<K,V>> action) {
4202 >        if (action == null) throw new NullPointerException();
4203 >        new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4204 >                                  action).invoke();
4205 >    }
4206 >
4207 >    /**
4208 >     * Performs the given action for each non-null transformation
4209 >     * of each entry.
4210 >     *
4211 >     * @param parallelismThreshold the (estimated) number of elements
4212 >     * needed for this operation to be executed in parallel
4213 >     * @param transformer a function returning the transformation
4214 >     * for an element, or null if there is no transformation (in
4215 >     * which case the action is not applied)
4216 >     * @param action the action
4217 >     * @param <U> the return type of the transformer
4218 >     * @since 1.8
4219 >     */
4220 >    public <U> void forEachEntry(long parallelismThreshold,
4221 >                                 Function<Map.Entry<K,V>, ? extends U> transformer,
4222 >                                 Consumer<? super U> action) {
4223 >        if (transformer == null || action == null)
4224 >            throw new NullPointerException();
4225 >        new ForEachTransformedEntryTask<K,V,U>
4226 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4227 >             transformer, action).invoke();
4228 >    }
4229 >
4230 >    /**
4231 >     * Returns a non-null result from applying the given search
4232 >     * function on each entry, or null if none.  Upon success,
4233 >     * further element processing is suppressed and the results of
4234 >     * any other parallel invocations of the search function are
4235 >     * ignored.
4236 >     *
4237 >     * @param parallelismThreshold the (estimated) number of elements
4238 >     * needed for this operation to be executed in parallel
4239 >     * @param searchFunction a function returning a non-null
4240 >     * result on success, else null
4241 >     * @param <U> the return type of the search function
4242 >     * @return a non-null result from applying the given search
4243 >     * function on each entry, or null if none
4244 >     * @since 1.8
4245 >     */
4246 >    public <U> U searchEntries(long parallelismThreshold,
4247 >                               Function<Map.Entry<K,V>, ? extends U> searchFunction) {
4248 >        if (searchFunction == null) throw new NullPointerException();
4249 >        return new SearchEntriesTask<K,V,U>
4250 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4251 >             searchFunction, new AtomicReference<U>()).invoke();
4252 >    }
4253 >
4254 >    /**
4255 >     * Returns the result of accumulating all entries using the
4256 >     * given reducer to combine values, or null if none.
4257 >     *
4258 >     * @param parallelismThreshold the (estimated) number of elements
4259 >     * needed for this operation to be executed in parallel
4260 >     * @param reducer a commutative associative combining function
4261 >     * @return the result of accumulating all entries
4262 >     * @since 1.8
4263 >     */
4264 >    public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4265 >                                        BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4266 >        if (reducer == null) throw new NullPointerException();
4267 >        return new ReduceEntriesTask<K,V>
4268 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4269 >             null, reducer).invoke();
4270 >    }
4271 >
4272 >    /**
4273 >     * Returns the result of accumulating the given transformation
4274 >     * of all entries using the given reducer to combine values,
4275 >     * or null if none.
4276 >     *
4277 >     * @param parallelismThreshold the (estimated) number of elements
4278 >     * needed for this operation to be executed in parallel
4279 >     * @param transformer a function returning the transformation
4280 >     * for an element, or null if there is no transformation (in
4281 >     * which case it is not combined)
4282 >     * @param reducer a commutative associative combining function
4283 >     * @param <U> the return type of the transformer
4284 >     * @return the result of accumulating the given transformation
4285 >     * of all entries
4286 >     * @since 1.8
4287 >     */
4288 >    public <U> U reduceEntries(long parallelismThreshold,
4289 >                               Function<Map.Entry<K,V>, ? extends U> transformer,
4290 >                               BiFunction<? super U, ? super U, ? extends U> reducer) {
4291 >        if (transformer == null || reducer == null)
4292 >            throw new NullPointerException();
4293 >        return new MapReduceEntriesTask<K,V,U>
4294 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4295 >             null, transformer, reducer).invoke();
4296 >    }
4297 >
4298 >    /**
4299 >     * Returns the result of accumulating the given transformation
4300 >     * of all entries using the given reducer to combine values,
4301 >     * and the given basis as an identity value.
4302 >     *
4303 >     * @param parallelismThreshold the (estimated) number of elements
4304 >     * needed for this operation to be executed in parallel
4305 >     * @param transformer a function returning the transformation
4306 >     * for an element
4307 >     * @param basis the identity (initial default value) for the reduction
4308 >     * @param reducer a commutative associative combining function
4309 >     * @return the result of accumulating the given transformation
4310 >     * of all entries
4311 >     * @since 1.8
4312 >     */
4313 >    public double reduceEntriesToDouble(long parallelismThreshold,
4314 >                                        ToDoubleFunction<Map.Entry<K,V>> transformer,
4315 >                                        double basis,
4316 >                                        DoubleBinaryOperator reducer) {
4317 >        if (transformer == null || reducer == null)
4318 >            throw new NullPointerException();
4319 >        return new MapReduceEntriesToDoubleTask<K,V>
4320 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4321 >             null, transformer, basis, reducer).invoke();
4322 >    }
4323 >
4324 >    /**
4325 >     * Returns the result of accumulating the given transformation
4326 >     * of all entries using the given reducer to combine values,
4327 >     * and the given basis as an identity value.
4328 >     *
4329 >     * @param parallelismThreshold the (estimated) number of elements
4330 >     * needed for this operation to be executed in parallel
4331 >     * @param transformer a function returning the transformation
4332 >     * for an element
4333 >     * @param basis the identity (initial default value) for the reduction
4334 >     * @param reducer a commutative associative combining function
4335 >     * @return the result of accumulating the given transformation
4336 >     * of all entries
4337 >     * @since 1.8
4338 >     */
4339 >    public long reduceEntriesToLong(long parallelismThreshold,
4340 >                                    ToLongFunction<Map.Entry<K,V>> transformer,
4341 >                                    long basis,
4342 >                                    LongBinaryOperator reducer) {
4343 >        if (transformer == null || reducer == null)
4344 >            throw new NullPointerException();
4345 >        return new MapReduceEntriesToLongTask<K,V>
4346 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4347 >             null, transformer, basis, reducer).invoke();
4348 >    }
4349 >
4350 >    /**
4351 >     * Returns the result of accumulating the given transformation
4352 >     * of all entries using the given reducer to combine values,
4353 >     * and the given basis as an identity value.
4354 >     *
4355 >     * @param parallelismThreshold the (estimated) number of elements
4356 >     * needed for this operation to be executed in parallel
4357 >     * @param transformer a function returning the transformation
4358 >     * for an element
4359 >     * @param basis the identity (initial default value) for the reduction
4360 >     * @param reducer a commutative associative combining function
4361 >     * @return the result of accumulating the given transformation
4362 >     * of all entries
4363 >     * @since 1.8
4364 >     */
4365 >    public int reduceEntriesToInt(long parallelismThreshold,
4366 >                                  ToIntFunction<Map.Entry<K,V>> transformer,
4367 >                                  int basis,
4368 >                                  IntBinaryOperator reducer) {
4369 >        if (transformer == null || reducer == null)
4370 >            throw new NullPointerException();
4371 >        return new MapReduceEntriesToIntTask<K,V>
4372 >            (null, batchFor(parallelismThreshold), 0, 0, table,
4373 >             null, transformer, basis, reducer).invoke();
4374 >    }
4375 >
4376 >
4377 >    /* ----------------Views -------------- */
4378 >
4379 >    /**
4380 >     * Base class for views.
4381 >     */
4382 >    abstract static class CollectionView<K,V,E>
4383 >        implements Collection<E>, java.io.Serializable {
4384 >        private static final long serialVersionUID = 7249069246763182397L;
4385 >        final ConcurrentHashMap<K,V> map;
4386 >        CollectionView(ConcurrentHashMap<K,V> map)  { this.map = map; }
4387 >
4388 >        /**
4389 >         * Returns the map backing this view.
4390 >         *
4391 >         * @return the map backing this view
4392 >         */
4393 >        public ConcurrentHashMap<K,V> getMap() { return map; }
4394 >
4395 >        /**
4396 >         * Removes all of the elements from this view, by removing all
4397 >         * the mappings from the map backing this view.
4398 >         */
4399 >        public final void clear()      { map.clear(); }
4400 >        public final int size()        { return map.size(); }
4401 >        public final boolean isEmpty() { return map.isEmpty(); }
4402 >
4403 >        // implementations below rely on concrete classes supplying these
4404 >        // abstract methods
4405 >        /**
4406 >         * Returns an iterator over the elements in this collection.
4407 >         *
4408 >         * <p>The returned iterator is
4409 >         * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
4410 >         *
4411 >         * @return an iterator over the elements in this collection
4412 >         */
4413 >        public abstract Iterator<E> iterator();
4414 >        public abstract boolean contains(Object o);
4415 >        public abstract boolean remove(Object o);
4416 >
4417 >        private static final String OOME_MSG = "Required array size too large";
4418 >
4419 >        public final Object[] toArray() {
4420 >            long sz = map.mappingCount();
4421 >            if (sz > MAX_ARRAY_SIZE)
4422 >                throw new OutOfMemoryError(OOME_MSG);
4423 >            int n = (int)sz;
4424 >            Object[] r = new Object[n];
4425 >            int i = 0;
4426 >            for (E e : this) {
4427 >                if (i == n) {
4428 >                    if (n >= MAX_ARRAY_SIZE)
4429 >                        throw new OutOfMemoryError(OOME_MSG);
4430 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4431 >                        n = MAX_ARRAY_SIZE;
4432 >                    else
4433 >                        n += (n >>> 1) + 1;
4434 >                    r = Arrays.copyOf(r, n);
4435 >                }
4436 >                r[i++] = e;
4437 >            }
4438 >            return (i == n) ? r : Arrays.copyOf(r, i);
4439 >        }
4440 >
4441 >        @SuppressWarnings("unchecked")
4442 >        public final <T> T[] toArray(T[] a) {
4443 >            long sz = map.mappingCount();
4444 >            if (sz > MAX_ARRAY_SIZE)
4445 >                throw new OutOfMemoryError(OOME_MSG);
4446 >            int m = (int)sz;
4447 >            T[] r = (a.length >= m) ? a :
4448 >                (T[])java.lang.reflect.Array
4449 >                .newInstance(a.getClass().getComponentType(), m);
4450 >            int n = r.length;
4451 >            int i = 0;
4452 >            for (E e : this) {
4453 >                if (i == n) {
4454 >                    if (n >= MAX_ARRAY_SIZE)
4455 >                        throw new OutOfMemoryError(OOME_MSG);
4456 >                    if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4457 >                        n = MAX_ARRAY_SIZE;
4458 >                    else
4459 >                        n += (n >>> 1) + 1;
4460 >                    r = Arrays.copyOf(r, n);
4461 >                }
4462 >                r[i++] = (T)e;
4463 >            }
4464 >            if (a == r && i < n) {
4465 >                r[i] = null; // null-terminate
4466 >                return r;
4467 >            }
4468 >            return (i == n) ? r : Arrays.copyOf(r, i);
4469 >        }
4470 >
4471 >        /**
4472 >         * Returns a string representation of this collection.
4473 >         * The string representation consists of the string representations
4474 >         * of the collection's elements in the order they are returned by
4475 >         * its iterator, enclosed in square brackets ({@code "[]"}).
4476 >         * Adjacent elements are separated by the characters {@code ", "}
4477 >         * (comma and space).  Elements are converted to strings as by
4478 >         * {@link String#valueOf(Object)}.
4479 >         *
4480 >         * @return a string representation of this collection
4481 >         */
4482 >        public final String toString() {
4483 >            StringBuilder sb = new StringBuilder();
4484 >            sb.append('[');
4485 >            Iterator<E> it = iterator();
4486 >            if (it.hasNext()) {
4487 >                for (;;) {
4488 >                    Object e = it.next();
4489 >                    sb.append(e == this ? "(this Collection)" : e);
4490 >                    if (!it.hasNext())
4491 >                        break;
4492 >                    sb.append(',').append(' ');
4493 >                }
4494 >            }
4495 >            return sb.append(']').toString();
4496          }
4497 +
4498 +        public final boolean containsAll(Collection<?> c) {
4499 +            if (c != this) {
4500 +                for (Object e : c) {
4501 +                    if (e == null || !contains(e))
4502 +                        return false;
4503 +                }
4504 +            }
4505 +            return true;
4506 +        }
4507 +
4508 +        public boolean removeAll(Collection<?> c) {
4509 +            if (c == null) throw new NullPointerException();
4510 +            boolean modified = false;
4511 +            // Use (c instanceof Set) as a hint that lookup in c is as
4512 +            // efficient as this view
4513 +            Node<K,V>[] t;
4514 +            if ((t = map.table) == null) {
4515 +                return false;
4516 +            } else if (c instanceof Set<?> && c.size() > t.length) {
4517 +                for (Iterator<?> it = iterator(); it.hasNext(); ) {
4518 +                    if (c.contains(it.next())) {
4519 +                        it.remove();
4520 +                        modified = true;
4521 +                    }
4522 +                }
4523 +            } else {
4524 +                for (Object e : c)
4525 +                    modified |= remove(e);
4526 +            }
4527 +            return modified;
4528 +        }
4529 +
4530 +        public final boolean retainAll(Collection<?> c) {
4531 +            if (c == null) throw new NullPointerException();
4532 +            boolean modified = false;
4533 +            for (Iterator<E> it = iterator(); it.hasNext();) {
4534 +                if (!c.contains(it.next())) {
4535 +                    it.remove();
4536 +                    modified = true;
4537 +                }
4538 +            }
4539 +            return modified;
4540 +        }
4541 +
4542      }
4543  
4544 <    final class Values extends AbstractCollection<V> {
4545 <        public Iterator<V> iterator() {
4546 <            return new ValueIterator();
4544 >    /**
4545 >     * A view of a ConcurrentHashMap as a {@link Set} of keys, in
4546 >     * which additions may optionally be enabled by mapping to a
4547 >     * common value.  This class cannot be directly instantiated.
4548 >     * See {@link #keySet() keySet()},
4549 >     * {@link #keySet(Object) keySet(V)},
4550 >     * {@link #newKeySet() newKeySet()},
4551 >     * {@link #newKeySet(int) newKeySet(int)}.
4552 >     *
4553 >     * @since 1.8
4554 >     */
4555 >    public static class KeySetView<K,V> extends CollectionView<K,V,K>
4556 >        implements Set<K>, java.io.Serializable {
4557 >        private static final long serialVersionUID = 7249069246763182397L;
4558 >        private final V value;
4559 >        KeySetView(ConcurrentHashMap<K,V> map, V value) {  // non-public
4560 >            super(map);
4561 >            this.value = value;
4562 >        }
4563 >
4564 >        /**
4565 >         * Returns the default mapped value for additions,
4566 >         * or {@code null} if additions are not supported.
4567 >         *
4568 >         * @return the default mapped value for additions, or {@code null}
4569 >         * if not supported
4570 >         */
4571 >        public V getMappedValue() { return value; }
4572 >
4573 >        /**
4574 >         * {@inheritDoc}
4575 >         * @throws NullPointerException if the specified key is null
4576 >         */
4577 >        public boolean contains(Object o) { return map.containsKey(o); }
4578 >
4579 >        /**
4580 >         * Removes the key from this map view, by removing the key (and its
4581 >         * corresponding value) from the backing map.  This method does
4582 >         * nothing if the key is not in the map.
4583 >         *
4584 >         * @param  o the key to be removed from the backing map
4585 >         * @return {@code true} if the backing map contained the specified key
4586 >         * @throws NullPointerException if the specified key is null
4587 >         */
4588 >        public boolean remove(Object o) { return map.remove(o) != null; }
4589 >
4590 >        /**
4591 >         * @return an iterator over the keys of the backing map
4592 >         */
4593 >        public Iterator<K> iterator() {
4594 >            Node<K,V>[] t;
4595 >            ConcurrentHashMap<K,V> m = map;
4596 >            int f = (t = m.table) == null ? 0 : t.length;
4597 >            return new KeyIterator<K,V>(t, f, 0, f, m);
4598          }
4599 <        public int size() {
4600 <            return ConcurrentHashMap.this.size();
4599 >
4600 >        /**
4601 >         * Adds the specified key to this set view by mapping the key to
4602 >         * the default mapped value in the backing map, if defined.
4603 >         *
4604 >         * @param e key to be added
4605 >         * @return {@code true} if this set changed as a result of the call
4606 >         * @throws NullPointerException if the specified key is null
4607 >         * @throws UnsupportedOperationException if no default mapped value
4608 >         * for additions was provided
4609 >         */
4610 >        public boolean add(K e) {
4611 >            V v;
4612 >            if ((v = value) == null)
4613 >                throw new UnsupportedOperationException();
4614 >            return map.putVal(e, v, true) == null;
4615          }
4616 <        public boolean contains(Object o) {
4617 <            return ConcurrentHashMap.this.containsValue(o);
4616 >
4617 >        /**
4618 >         * Adds all of the elements in the specified collection to this set,
4619 >         * as if by calling {@link #add} on each one.
4620 >         *
4621 >         * @param c the elements to be inserted into this set
4622 >         * @return {@code true} if this set changed as a result of the call
4623 >         * @throws NullPointerException if the collection or any of its
4624 >         * elements are {@code null}
4625 >         * @throws UnsupportedOperationException if no default mapped value
4626 >         * for additions was provided
4627 >         */
4628 >        public boolean addAll(Collection<? extends K> c) {
4629 >            boolean added = false;
4630 >            V v;
4631 >            if ((v = value) == null)
4632 >                throw new UnsupportedOperationException();
4633 >            for (K e : c) {
4634 >                if (map.putVal(e, v, true) == null)
4635 >                    added = true;
4636 >            }
4637 >            return added;
4638          }
4639 <        public void clear() {
4640 <            ConcurrentHashMap.this.clear();
4639 >
4640 >        public int hashCode() {
4641 >            int h = 0;
4642 >            for (K e : this)
4643 >                h += e.hashCode();
4644 >            return h;
4645 >        }
4646 >
4647 >        public boolean equals(Object o) {
4648 >            Set<?> c;
4649 >            return ((o instanceof Set) &&
4650 >                    ((c = (Set<?>)o) == this ||
4651 >                     (containsAll(c) && c.containsAll(this))));
4652 >        }
4653 >
4654 >        public Spliterator<K> spliterator() {
4655 >            Node<K,V>[] t;
4656 >            ConcurrentHashMap<K,V> m = map;
4657 >            long n = m.sumCount();
4658 >            int f = (t = m.table) == null ? 0 : t.length;
4659 >            return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4660 >        }
4661 >
4662 >        public void forEach(Consumer<? super K> action) {
4663 >            if (action == null) throw new NullPointerException();
4664 >            Node<K,V>[] t;
4665 >            if ((t = map.table) != null) {
4666 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4667 >                for (Node<K,V> p; (p = it.advance()) != null; )
4668 >                    action.accept(p.key);
4669 >            }
4670          }
4671      }
4672  
4673 <    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
4674 <        public Iterator<Map.Entry<K,V>> iterator() {
4675 <            return new EntryIterator();
4673 >    /**
4674 >     * A view of a ConcurrentHashMap as a {@link Collection} of
4675 >     * values, in which additions are disabled. This class cannot be
4676 >     * directly instantiated. See {@link #values()}.
4677 >     */
4678 >    static final class ValuesView<K,V> extends CollectionView<K,V,V>
4679 >        implements Collection<V>, java.io.Serializable {
4680 >        private static final long serialVersionUID = 2249069246763182397L;
4681 >        ValuesView(ConcurrentHashMap<K,V> map) { super(map); }
4682 >        public final boolean contains(Object o) {
4683 >            return map.containsValue(o);
4684 >        }
4685 >
4686 >        public final boolean remove(Object o) {
4687 >            if (o != null) {
4688 >                for (Iterator<V> it = iterator(); it.hasNext();) {
4689 >                    if (o.equals(it.next())) {
4690 >                        it.remove();
4691 >                        return true;
4692 >                    }
4693 >                }
4694 >            }
4695 >            return false;
4696 >        }
4697 >
4698 >        public final Iterator<V> iterator() {
4699 >            ConcurrentHashMap<K,V> m = map;
4700 >            Node<K,V>[] t;
4701 >            int f = (t = m.table) == null ? 0 : t.length;
4702 >            return new ValueIterator<K,V>(t, f, 0, f, m);
4703 >        }
4704 >
4705 >        public final boolean add(V e) {
4706 >            throw new UnsupportedOperationException();
4707 >        }
4708 >        public final boolean addAll(Collection<? extends V> c) {
4709 >            throw new UnsupportedOperationException();
4710 >        }
4711 >
4712 >        @Override public boolean removeAll(Collection<?> c) {
4713 >            if (c == null) throw new NullPointerException();
4714 >            boolean modified = false;
4715 >            for (Iterator<V> it = iterator(); it.hasNext();) {
4716 >                if (c.contains(it.next())) {
4717 >                    it.remove();
4718 >                    modified = true;
4719 >                }
4720 >            }
4721 >            return modified;
4722 >        }
4723 >
4724 >        public boolean removeIf(Predicate<? super V> filter) {
4725 >            return map.removeValueIf(filter);
4726 >        }
4727 >
4728 >        public Spliterator<V> spliterator() {
4729 >            Node<K,V>[] t;
4730 >            ConcurrentHashMap<K,V> m = map;
4731 >            long n = m.sumCount();
4732 >            int f = (t = m.table) == null ? 0 : t.length;
4733 >            return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4734 >        }
4735 >
4736 >        public void forEach(Consumer<? super V> action) {
4737 >            if (action == null) throw new NullPointerException();
4738 >            Node<K,V>[] t;
4739 >            if ((t = map.table) != null) {
4740 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4741 >                for (Node<K,V> p; (p = it.advance()) != null; )
4742 >                    action.accept(p.val);
4743 >            }
4744          }
4745 +    }
4746 +
4747 +    /**
4748 +     * A view of a ConcurrentHashMap as a {@link Set} of (key, value)
4749 +     * entries.  This class cannot be directly instantiated. See
4750 +     * {@link #entrySet()}.
4751 +     */
4752 +    static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4753 +        implements Set<Map.Entry<K,V>>, java.io.Serializable {
4754 +        private static final long serialVersionUID = 2249069246763182397L;
4755 +        EntrySetView(ConcurrentHashMap<K,V> map) { super(map); }
4756 +
4757          public boolean contains(Object o) {
4758 <            if (!(o instanceof Map.Entry))
4759 <                return false;
4760 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
4761 <            V v = ConcurrentHashMap.this.get(e.getKey());
4762 <            return v != null && v.equals(e.getValue());
4758 >            Object k, v, r; Map.Entry<?,?> e;
4759 >            return ((o instanceof Map.Entry) &&
4760 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4761 >                    (r = map.get(k)) != null &&
4762 >                    (v = e.getValue()) != null &&
4763 >                    (v == r || v.equals(r)));
4764          }
4765 +
4766          public boolean remove(Object o) {
4767 <            if (!(o instanceof Map.Entry))
4768 <                return false;
4769 <            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
4770 <            return ConcurrentHashMap.this.remove(e.getKey(), e.getValue());
4767 >            Object k, v; Map.Entry<?,?> e;
4768 >            return ((o instanceof Map.Entry) &&
4769 >                    (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4770 >                    (v = e.getValue()) != null &&
4771 >                    map.remove(k, v));
4772 >        }
4773 >
4774 >        /**
4775 >         * @return an iterator over the entries of the backing map
4776 >         */
4777 >        public Iterator<Map.Entry<K,V>> iterator() {
4778 >            ConcurrentHashMap<K,V> m = map;
4779 >            Node<K,V>[] t;
4780 >            int f = (t = m.table) == null ? 0 : t.length;
4781 >            return new EntryIterator<K,V>(t, f, 0, f, m);
4782 >        }
4783 >
4784 >        public boolean add(Entry<K,V> e) {
4785 >            return map.putVal(e.getKey(), e.getValue(), false) == null;
4786 >        }
4787 >
4788 >        public boolean addAll(Collection<? extends Entry<K,V>> c) {
4789 >            boolean added = false;
4790 >            for (Entry<K,V> e : c) {
4791 >                if (add(e))
4792 >                    added = true;
4793 >            }
4794 >            return added;
4795 >        }
4796 >
4797 >        public boolean removeIf(Predicate<? super Entry<K,V>> filter) {
4798 >            return map.removeEntryIf(filter);
4799          }
4800 <        public int size() {
4801 <            return ConcurrentHashMap.this.size();
4800 >
4801 >        public final int hashCode() {
4802 >            int h = 0;
4803 >            Node<K,V>[] t;
4804 >            if ((t = map.table) != null) {
4805 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4806 >                for (Node<K,V> p; (p = it.advance()) != null; ) {
4807 >                    h += p.hashCode();
4808 >                }
4809 >            }
4810 >            return h;
4811          }
4812 <        public void clear() {
4813 <            ConcurrentHashMap.this.clear();
4812 >
4813 >        public final boolean equals(Object o) {
4814 >            Set<?> c;
4815 >            return ((o instanceof Set) &&
4816 >                    ((c = (Set<?>)o) == this ||
4817 >                     (containsAll(c) && c.containsAll(this))));
4818 >        }
4819 >
4820 >        public Spliterator<Map.Entry<K,V>> spliterator() {
4821 >            Node<K,V>[] t;
4822 >            ConcurrentHashMap<K,V> m = map;
4823 >            long n = m.sumCount();
4824 >            int f = (t = m.table) == null ? 0 : t.length;
4825 >            return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4826 >        }
4827 >
4828 >        public void forEach(Consumer<? super Map.Entry<K,V>> action) {
4829 >            if (action == null) throw new NullPointerException();
4830 >            Node<K,V>[] t;
4831 >            if ((t = map.table) != null) {
4832 >                Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4833 >                for (Node<K,V> p; (p = it.advance()) != null; )
4834 >                    action.accept(new MapEntry<K,V>(p.key, p.val, map));
4835 >            }
4836          }
4837 +
4838      }
4839  
4840 <    /* ---------------- Serialization Support -------------- */
4840 >    // -------------------------------------------------------
4841  
4842      /**
4843 <     * Save the state of the <tt>ConcurrentHashMap</tt> instance to a
4844 <     * stream (i.e., serialize it).
1229 <     * @param s the stream
1230 <     * @serialData
1231 <     * the key (Object) and value (Object)
1232 <     * for each key-value mapping, followed by a null pair.
1233 <     * The key-value mappings are emitted in no particular order.
4843 >     * Base class for bulk tasks. Repeats some fields and code from
4844 >     * class Traverser, because we need to subclass CountedCompleter.
4845       */
4846 <    private void writeObject(java.io.ObjectOutputStream s) throws IOException  {
4847 <        s.defaultWriteObject();
4846 >    @SuppressWarnings("serial")
4847 >    abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4848 >        Node<K,V>[] tab;        // same as Traverser
4849 >        Node<K,V> next;
4850 >        TableStack<K,V> stack, spare;
4851 >        int index;
4852 >        int baseIndex;
4853 >        int baseLimit;
4854 >        final int baseSize;
4855 >        int batch;              // split control
4856 >
4857 >        BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4858 >            super(par);
4859 >            this.batch = b;
4860 >            this.index = this.baseIndex = i;
4861 >            if ((this.tab = t) == null)
4862 >                this.baseSize = this.baseLimit = 0;
4863 >            else if (par == null)
4864 >                this.baseSize = this.baseLimit = t.length;
4865 >            else {
4866 >                this.baseLimit = f;
4867 >                this.baseSize = par.baseSize;
4868 >            }
4869 >        }
4870  
4871 <        for (int k = 0; k < segments.length; ++k) {
4872 <            Segment<K,V> seg = segments[k];
4873 <            seg.lock();
4874 <            try {
4875 <                HashEntry<K,V>[] tab = seg.table;
4876 <                for (int i = 0; i < tab.length; ++i) {
4877 <                    for (HashEntry<K,V> e = tab[i]; e != null; e = e.next) {
4878 <                        s.writeObject(e.key);
4879 <                        s.writeObject(e.value);
4871 >        /**
4872 >         * Same as Traverser version.
4873 >         */
4874 >        final Node<K,V> advance() {
4875 >            Node<K,V> e;
4876 >            if ((e = next) != null)
4877 >                e = e.next;
4878 >            for (;;) {
4879 >                Node<K,V>[] t; int i, n;
4880 >                if (e != null)
4881 >                    return next = e;
4882 >                if (baseIndex >= baseLimit || (t = tab) == null ||
4883 >                    (n = t.length) <= (i = index) || i < 0)
4884 >                    return next = null;
4885 >                if ((e = tabAt(t, i)) != null && e.hash < 0) {
4886 >                    if (e instanceof ForwardingNode) {
4887 >                        tab = ((ForwardingNode<K,V>)e).nextTable;
4888 >                        e = null;
4889 >                        pushState(t, i, n);
4890 >                        continue;
4891                      }
4892 +                    else if (e instanceof TreeBin)
4893 +                        e = ((TreeBin<K,V>)e).first;
4894 +                    else
4895 +                        e = null;
4896                  }
4897 <            } finally {
4898 <                seg.unlock();
4897 >                if (stack != null)
4898 >                    recoverState(n);
4899 >                else if ((index = i + baseSize) >= n)
4900 >                    index = ++baseIndex;
4901              }
4902          }
4903 <        s.writeObject(null);
4904 <        s.writeObject(null);
4903 >
4904 >        private void pushState(Node<K,V>[] t, int i, int n) {
4905 >            TableStack<K,V> s = spare;
4906 >            if (s != null)
4907 >                spare = s.next;
4908 >            else
4909 >                s = new TableStack<K,V>();
4910 >            s.tab = t;
4911 >            s.length = n;
4912 >            s.index = i;
4913 >            s.next = stack;
4914 >            stack = s;
4915 >        }
4916 >
4917 >        private void recoverState(int n) {
4918 >            TableStack<K,V> s; int len;
4919 >            while ((s = stack) != null && (index += (len = s.length)) >= n) {
4920 >                n = len;
4921 >                index = s.index;
4922 >                tab = s.tab;
4923 >                s.tab = null;
4924 >                TableStack<K,V> next = s.next;
4925 >                s.next = spare; // save for reuse
4926 >                stack = next;
4927 >                spare = s;
4928 >            }
4929 >            if (s == null && (index += baseSize) >= n)
4930 >                index = ++baseIndex;
4931 >        }
4932      }
4933  
4934 <    /**
4935 <     * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a
4936 <     * stream (i.e., deserialize it).
4937 <     * @param s the stream
4938 <     */
4939 <    private void readObject(java.io.ObjectInputStream s)
4940 <        throws IOException, ClassNotFoundException  {
4941 <        s.defaultReadObject();
4934 >    /*
4935 >     * Task classes. Coded in a regular but ugly format/style to
4936 >     * simplify checks that each variant differs in the right way from
4937 >     * others. The null screenings exist because compilers cannot tell
4938 >     * that we've already null-checked task arguments, so we force
4939 >     * simplest hoisted bypass to help avoid convoluted traps.
4940 >     */
4941 >    @SuppressWarnings("serial")
4942 >    static final class ForEachKeyTask<K,V>
4943 >        extends BulkTask<K,V,Void> {
4944 >        final Consumer<? super K> action;
4945 >        ForEachKeyTask
4946 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4947 >             Consumer<? super K> action) {
4948 >            super(p, b, i, f, t);
4949 >            this.action = action;
4950 >        }
4951 >        public final void compute() {
4952 >            final Consumer<? super K> action;
4953 >            if ((action = this.action) != null) {
4954 >                for (int i = baseIndex, f, h; batch > 0 &&
4955 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4956 >                    addToPendingCount(1);
4957 >                    new ForEachKeyTask<K,V>
4958 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4959 >                         action).fork();
4960 >                }
4961 >                for (Node<K,V> p; (p = advance()) != null;)
4962 >                    action.accept(p.key);
4963 >                propagateCompletion();
4964 >            }
4965 >        }
4966 >    }
4967  
4968 <        // Initialize each segment to be minimally sized, and let grow.
4969 <        for (int i = 0; i < segments.length; ++i) {
4970 <            segments[i].setTable(new HashEntry[1]);
4968 >    @SuppressWarnings("serial")
4969 >    static final class ForEachValueTask<K,V>
4970 >        extends BulkTask<K,V,Void> {
4971 >        final Consumer<? super V> action;
4972 >        ForEachValueTask
4973 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4974 >             Consumer<? super V> action) {
4975 >            super(p, b, i, f, t);
4976 >            this.action = action;
4977 >        }
4978 >        public final void compute() {
4979 >            final Consumer<? super V> action;
4980 >            if ((action = this.action) != null) {
4981 >                for (int i = baseIndex, f, h; batch > 0 &&
4982 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
4983 >                    addToPendingCount(1);
4984 >                    new ForEachValueTask<K,V>
4985 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
4986 >                         action).fork();
4987 >                }
4988 >                for (Node<K,V> p; (p = advance()) != null;)
4989 >                    action.accept(p.val);
4990 >                propagateCompletion();
4991 >            }
4992          }
4993 +    }
4994  
4995 <        // Read the keys and values, and put the mappings in the table
4996 <        for (;;) {
4997 <            K key = (K) s.readObject();
4998 <            V value = (V) s.readObject();
4999 <            if (key == null)
5000 <                break;
5001 <            put(key, value);
4995 >    @SuppressWarnings("serial")
4996 >    static final class ForEachEntryTask<K,V>
4997 >        extends BulkTask<K,V,Void> {
4998 >        final Consumer<? super Entry<K,V>> action;
4999 >        ForEachEntryTask
5000 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5001 >             Consumer<? super Entry<K,V>> action) {
5002 >            super(p, b, i, f, t);
5003 >            this.action = action;
5004 >        }
5005 >        public final void compute() {
5006 >            final Consumer<? super Entry<K,V>> action;
5007 >            if ((action = this.action) != null) {
5008 >                for (int i = baseIndex, f, h; batch > 0 &&
5009 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5010 >                    addToPendingCount(1);
5011 >                    new ForEachEntryTask<K,V>
5012 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5013 >                         action).fork();
5014 >                }
5015 >                for (Node<K,V> p; (p = advance()) != null; )
5016 >                    action.accept(p);
5017 >                propagateCompletion();
5018 >            }
5019 >        }
5020 >    }
5021 >
5022 >    @SuppressWarnings("serial")
5023 >    static final class ForEachMappingTask<K,V>
5024 >        extends BulkTask<K,V,Void> {
5025 >        final BiConsumer<? super K, ? super V> action;
5026 >        ForEachMappingTask
5027 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5028 >             BiConsumer<? super K,? super V> action) {
5029 >            super(p, b, i, f, t);
5030 >            this.action = action;
5031 >        }
5032 >        public final void compute() {
5033 >            final BiConsumer<? super K, ? super V> action;
5034 >            if ((action = this.action) != null) {
5035 >                for (int i = baseIndex, f, h; batch > 0 &&
5036 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5037 >                    addToPendingCount(1);
5038 >                    new ForEachMappingTask<K,V>
5039 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5040 >                         action).fork();
5041 >                }
5042 >                for (Node<K,V> p; (p = advance()) != null; )
5043 >                    action.accept(p.key, p.val);
5044 >                propagateCompletion();
5045 >            }
5046 >        }
5047 >    }
5048 >
5049 >    @SuppressWarnings("serial")
5050 >    static final class ForEachTransformedKeyTask<K,V,U>
5051 >        extends BulkTask<K,V,Void> {
5052 >        final Function<? super K, ? extends U> transformer;
5053 >        final Consumer<? super U> action;
5054 >        ForEachTransformedKeyTask
5055 >            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5056 >             Function<? super K, ? extends U> transformer, Consumer<? super U> action) {
5057 >            super(p, b, i, f, t);
5058 >            this.transformer = transformer; this.action = action;
5059 >        }
5060 >        public final void compute() {
5061 >            final Function<? super K, ? extends U> transformer;
5062 >            final Consumer<? super U> action;
5063 >            if ((transformer = this.transformer) != null &&
5064 >                (action = this.action) != null) {
5065 >                for (int i = baseIndex, f, h; batch > 0 &&
5066 >                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5067 >                    addToPendingCount(1);
5068 >                    new ForEachTransformedKeyTask<K,V,U>
5069 >                        (this, batch >>>= 1, baseLimit = h, f, tab,
5070 >                         transformer, action).fork();
5071 >                }
5072 >                for (Node<K,V> p; (p = advance()) != null; ) {
5073 >                    U u;
5074 >                    if ((u = transformer.apply(p.key)) != null)
5075 >                        action.accept(u);
5076 >                }
5077 >                propagateCompletion();
5078 >            }
5079          }
5080      }
5081 +
5082 +    @SuppressWarnings("serial")
5083 +    static final class ForEachTransformedValueTask<K,V,U>
5084 +        extends BulkTask<K,V,Void> {
5085 +        final Function<? super V, ? extends U> transformer;
5086 +        final Consumer<? super U> action;
5087 +        ForEachTransformedValueTask
5088 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5089 +             Function<? super V, ? extends U> transformer, Consumer<? super U> action) {
5090 +            super(p, b, i, f, t);
5091 +            this.transformer = transformer; this.action = action;
5092 +        }
5093 +        public final void compute() {
5094 +            final Function<? super V, ? extends U> transformer;
5095 +            final Consumer<? super U> action;
5096 +            if ((transformer = this.transformer) != null &&
5097 +                (action = this.action) != null) {
5098 +                for (int i = baseIndex, f, h; batch > 0 &&
5099 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5100 +                    addToPendingCount(1);
5101 +                    new ForEachTransformedValueTask<K,V,U>
5102 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5103 +                         transformer, action).fork();
5104 +                }
5105 +                for (Node<K,V> p; (p = advance()) != null; ) {
5106 +                    U u;
5107 +                    if ((u = transformer.apply(p.val)) != null)
5108 +                        action.accept(u);
5109 +                }
5110 +                propagateCompletion();
5111 +            }
5112 +        }
5113 +    }
5114 +
5115 +    @SuppressWarnings("serial")
5116 +    static final class ForEachTransformedEntryTask<K,V,U>
5117 +        extends BulkTask<K,V,Void> {
5118 +        final Function<Map.Entry<K,V>, ? extends U> transformer;
5119 +        final Consumer<? super U> action;
5120 +        ForEachTransformedEntryTask
5121 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5122 +             Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) {
5123 +            super(p, b, i, f, t);
5124 +            this.transformer = transformer; this.action = action;
5125 +        }
5126 +        public final void compute() {
5127 +            final Function<Map.Entry<K,V>, ? extends U> transformer;
5128 +            final Consumer<? super U> action;
5129 +            if ((transformer = this.transformer) != null &&
5130 +                (action = this.action) != null) {
5131 +                for (int i = baseIndex, f, h; batch > 0 &&
5132 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5133 +                    addToPendingCount(1);
5134 +                    new ForEachTransformedEntryTask<K,V,U>
5135 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5136 +                         transformer, action).fork();
5137 +                }
5138 +                for (Node<K,V> p; (p = advance()) != null; ) {
5139 +                    U u;
5140 +                    if ((u = transformer.apply(p)) != null)
5141 +                        action.accept(u);
5142 +                }
5143 +                propagateCompletion();
5144 +            }
5145 +        }
5146 +    }
5147 +
5148 +    @SuppressWarnings("serial")
5149 +    static final class ForEachTransformedMappingTask<K,V,U>
5150 +        extends BulkTask<K,V,Void> {
5151 +        final BiFunction<? super K, ? super V, ? extends U> transformer;
5152 +        final Consumer<? super U> action;
5153 +        ForEachTransformedMappingTask
5154 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5155 +             BiFunction<? super K, ? super V, ? extends U> transformer,
5156 +             Consumer<? super U> action) {
5157 +            super(p, b, i, f, t);
5158 +            this.transformer = transformer; this.action = action;
5159 +        }
5160 +        public final void compute() {
5161 +            final BiFunction<? super K, ? super V, ? extends U> transformer;
5162 +            final Consumer<? super U> action;
5163 +            if ((transformer = this.transformer) != null &&
5164 +                (action = this.action) != null) {
5165 +                for (int i = baseIndex, f, h; batch > 0 &&
5166 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5167 +                    addToPendingCount(1);
5168 +                    new ForEachTransformedMappingTask<K,V,U>
5169 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5170 +                         transformer, action).fork();
5171 +                }
5172 +                for (Node<K,V> p; (p = advance()) != null; ) {
5173 +                    U u;
5174 +                    if ((u = transformer.apply(p.key, p.val)) != null)
5175 +                        action.accept(u);
5176 +                }
5177 +                propagateCompletion();
5178 +            }
5179 +        }
5180 +    }
5181 +
5182 +    @SuppressWarnings("serial")
5183 +    static final class SearchKeysTask<K,V,U>
5184 +        extends BulkTask<K,V,U> {
5185 +        final Function<? super K, ? extends U> searchFunction;
5186 +        final AtomicReference<U> result;
5187 +        SearchKeysTask
5188 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5189 +             Function<? super K, ? extends U> searchFunction,
5190 +             AtomicReference<U> result) {
5191 +            super(p, b, i, f, t);
5192 +            this.searchFunction = searchFunction; this.result = result;
5193 +        }
5194 +        public final U getRawResult() { return result.get(); }
5195 +        public final void compute() {
5196 +            final Function<? super K, ? extends U> searchFunction;
5197 +            final AtomicReference<U> result;
5198 +            if ((searchFunction = this.searchFunction) != null &&
5199 +                (result = this.result) != null) {
5200 +                for (int i = baseIndex, f, h; batch > 0 &&
5201 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5202 +                    if (result.get() != null)
5203 +                        return;
5204 +                    addToPendingCount(1);
5205 +                    new SearchKeysTask<K,V,U>
5206 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5207 +                         searchFunction, result).fork();
5208 +                }
5209 +                while (result.get() == null) {
5210 +                    U u;
5211 +                    Node<K,V> p;
5212 +                    if ((p = advance()) == null) {
5213 +                        propagateCompletion();
5214 +                        break;
5215 +                    }
5216 +                    if ((u = searchFunction.apply(p.key)) != null) {
5217 +                        if (result.compareAndSet(null, u))
5218 +                            quietlyCompleteRoot();
5219 +                        break;
5220 +                    }
5221 +                }
5222 +            }
5223 +        }
5224 +    }
5225 +
5226 +    @SuppressWarnings("serial")
5227 +    static final class SearchValuesTask<K,V,U>
5228 +        extends BulkTask<K,V,U> {
5229 +        final Function<? super V, ? extends U> searchFunction;
5230 +        final AtomicReference<U> result;
5231 +        SearchValuesTask
5232 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5233 +             Function<? super V, ? extends U> searchFunction,
5234 +             AtomicReference<U> result) {
5235 +            super(p, b, i, f, t);
5236 +            this.searchFunction = searchFunction; this.result = result;
5237 +        }
5238 +        public final U getRawResult() { return result.get(); }
5239 +        public final void compute() {
5240 +            final Function<? super V, ? extends U> searchFunction;
5241 +            final AtomicReference<U> result;
5242 +            if ((searchFunction = this.searchFunction) != null &&
5243 +                (result = this.result) != null) {
5244 +                for (int i = baseIndex, f, h; batch > 0 &&
5245 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5246 +                    if (result.get() != null)
5247 +                        return;
5248 +                    addToPendingCount(1);
5249 +                    new SearchValuesTask<K,V,U>
5250 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5251 +                         searchFunction, result).fork();
5252 +                }
5253 +                while (result.get() == null) {
5254 +                    U u;
5255 +                    Node<K,V> p;
5256 +                    if ((p = advance()) == null) {
5257 +                        propagateCompletion();
5258 +                        break;
5259 +                    }
5260 +                    if ((u = searchFunction.apply(p.val)) != null) {
5261 +                        if (result.compareAndSet(null, u))
5262 +                            quietlyCompleteRoot();
5263 +                        break;
5264 +                    }
5265 +                }
5266 +            }
5267 +        }
5268 +    }
5269 +
5270 +    @SuppressWarnings("serial")
5271 +    static final class SearchEntriesTask<K,V,U>
5272 +        extends BulkTask<K,V,U> {
5273 +        final Function<Entry<K,V>, ? extends U> searchFunction;
5274 +        final AtomicReference<U> result;
5275 +        SearchEntriesTask
5276 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5277 +             Function<Entry<K,V>, ? extends U> searchFunction,
5278 +             AtomicReference<U> result) {
5279 +            super(p, b, i, f, t);
5280 +            this.searchFunction = searchFunction; this.result = result;
5281 +        }
5282 +        public final U getRawResult() { return result.get(); }
5283 +        public final void compute() {
5284 +            final Function<Entry<K,V>, ? extends U> searchFunction;
5285 +            final AtomicReference<U> result;
5286 +            if ((searchFunction = this.searchFunction) != null &&
5287 +                (result = this.result) != null) {
5288 +                for (int i = baseIndex, f, h; batch > 0 &&
5289 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5290 +                    if (result.get() != null)
5291 +                        return;
5292 +                    addToPendingCount(1);
5293 +                    new SearchEntriesTask<K,V,U>
5294 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5295 +                         searchFunction, result).fork();
5296 +                }
5297 +                while (result.get() == null) {
5298 +                    U u;
5299 +                    Node<K,V> p;
5300 +                    if ((p = advance()) == null) {
5301 +                        propagateCompletion();
5302 +                        break;
5303 +                    }
5304 +                    if ((u = searchFunction.apply(p)) != null) {
5305 +                        if (result.compareAndSet(null, u))
5306 +                            quietlyCompleteRoot();
5307 +                        return;
5308 +                    }
5309 +                }
5310 +            }
5311 +        }
5312 +    }
5313 +
5314 +    @SuppressWarnings("serial")
5315 +    static final class SearchMappingsTask<K,V,U>
5316 +        extends BulkTask<K,V,U> {
5317 +        final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5318 +        final AtomicReference<U> result;
5319 +        SearchMappingsTask
5320 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5321 +             BiFunction<? super K, ? super V, ? extends U> searchFunction,
5322 +             AtomicReference<U> result) {
5323 +            super(p, b, i, f, t);
5324 +            this.searchFunction = searchFunction; this.result = result;
5325 +        }
5326 +        public final U getRawResult() { return result.get(); }
5327 +        public final void compute() {
5328 +            final BiFunction<? super K, ? super V, ? extends U> searchFunction;
5329 +            final AtomicReference<U> result;
5330 +            if ((searchFunction = this.searchFunction) != null &&
5331 +                (result = this.result) != null) {
5332 +                for (int i = baseIndex, f, h; batch > 0 &&
5333 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5334 +                    if (result.get() != null)
5335 +                        return;
5336 +                    addToPendingCount(1);
5337 +                    new SearchMappingsTask<K,V,U>
5338 +                        (this, batch >>>= 1, baseLimit = h, f, tab,
5339 +                         searchFunction, result).fork();
5340 +                }
5341 +                while (result.get() == null) {
5342 +                    U u;
5343 +                    Node<K,V> p;
5344 +                    if ((p = advance()) == null) {
5345 +                        propagateCompletion();
5346 +                        break;
5347 +                    }
5348 +                    if ((u = searchFunction.apply(p.key, p.val)) != null) {
5349 +                        if (result.compareAndSet(null, u))
5350 +                            quietlyCompleteRoot();
5351 +                        break;
5352 +                    }
5353 +                }
5354 +            }
5355 +        }
5356 +    }
5357 +
5358 +    @SuppressWarnings("serial")
5359 +    static final class ReduceKeysTask<K,V>
5360 +        extends BulkTask<K,V,K> {
5361 +        final BiFunction<? super K, ? super K, ? extends K> reducer;
5362 +        K result;
5363 +        ReduceKeysTask<K,V> rights, nextRight;
5364 +        ReduceKeysTask
5365 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5366 +             ReduceKeysTask<K,V> nextRight,
5367 +             BiFunction<? super K, ? super K, ? extends K> reducer) {
5368 +            super(p, b, i, f, t); this.nextRight = nextRight;
5369 +            this.reducer = reducer;
5370 +        }
5371 +        public final K getRawResult() { return result; }
5372 +        public final void compute() {
5373 +            final BiFunction<? super K, ? super K, ? extends K> reducer;
5374 +            if ((reducer = this.reducer) != null) {
5375 +                for (int i = baseIndex, f, h; batch > 0 &&
5376 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5377 +                    addToPendingCount(1);
5378 +                    (rights = new ReduceKeysTask<K,V>
5379 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5380 +                      rights, reducer)).fork();
5381 +                }
5382 +                K r = null;
5383 +                for (Node<K,V> p; (p = advance()) != null; ) {
5384 +                    K u = p.key;
5385 +                    r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5386 +                }
5387 +                result = r;
5388 +                CountedCompleter<?> c;
5389 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5390 +                    @SuppressWarnings("unchecked")
5391 +                    ReduceKeysTask<K,V>
5392 +                        t = (ReduceKeysTask<K,V>)c,
5393 +                        s = t.rights;
5394 +                    while (s != null) {
5395 +                        K tr, sr;
5396 +                        if ((sr = s.result) != null)
5397 +                            t.result = (((tr = t.result) == null) ? sr :
5398 +                                        reducer.apply(tr, sr));
5399 +                        s = t.rights = s.nextRight;
5400 +                    }
5401 +                }
5402 +            }
5403 +        }
5404 +    }
5405 +
5406 +    @SuppressWarnings("serial")
5407 +    static final class ReduceValuesTask<K,V>
5408 +        extends BulkTask<K,V,V> {
5409 +        final BiFunction<? super V, ? super V, ? extends V> reducer;
5410 +        V result;
5411 +        ReduceValuesTask<K,V> rights, nextRight;
5412 +        ReduceValuesTask
5413 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5414 +             ReduceValuesTask<K,V> nextRight,
5415 +             BiFunction<? super V, ? super V, ? extends V> reducer) {
5416 +            super(p, b, i, f, t); this.nextRight = nextRight;
5417 +            this.reducer = reducer;
5418 +        }
5419 +        public final V getRawResult() { return result; }
5420 +        public final void compute() {
5421 +            final BiFunction<? super V, ? super V, ? extends V> reducer;
5422 +            if ((reducer = this.reducer) != null) {
5423 +                for (int i = baseIndex, f, h; batch > 0 &&
5424 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5425 +                    addToPendingCount(1);
5426 +                    (rights = new ReduceValuesTask<K,V>
5427 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5428 +                      rights, reducer)).fork();
5429 +                }
5430 +                V r = null;
5431 +                for (Node<K,V> p; (p = advance()) != null; ) {
5432 +                    V v = p.val;
5433 +                    r = (r == null) ? v : reducer.apply(r, v);
5434 +                }
5435 +                result = r;
5436 +                CountedCompleter<?> c;
5437 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5438 +                    @SuppressWarnings("unchecked")
5439 +                    ReduceValuesTask<K,V>
5440 +                        t = (ReduceValuesTask<K,V>)c,
5441 +                        s = t.rights;
5442 +                    while (s != null) {
5443 +                        V tr, sr;
5444 +                        if ((sr = s.result) != null)
5445 +                            t.result = (((tr = t.result) == null) ? sr :
5446 +                                        reducer.apply(tr, sr));
5447 +                        s = t.rights = s.nextRight;
5448 +                    }
5449 +                }
5450 +            }
5451 +        }
5452 +    }
5453 +
5454 +    @SuppressWarnings("serial")
5455 +    static final class ReduceEntriesTask<K,V>
5456 +        extends BulkTask<K,V,Map.Entry<K,V>> {
5457 +        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5458 +        Map.Entry<K,V> result;
5459 +        ReduceEntriesTask<K,V> rights, nextRight;
5460 +        ReduceEntriesTask
5461 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5462 +             ReduceEntriesTask<K,V> nextRight,
5463 +             BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5464 +            super(p, b, i, f, t); this.nextRight = nextRight;
5465 +            this.reducer = reducer;
5466 +        }
5467 +        public final Map.Entry<K,V> getRawResult() { return result; }
5468 +        public final void compute() {
5469 +            final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5470 +            if ((reducer = this.reducer) != null) {
5471 +                for (int i = baseIndex, f, h; batch > 0 &&
5472 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5473 +                    addToPendingCount(1);
5474 +                    (rights = new ReduceEntriesTask<K,V>
5475 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5476 +                      rights, reducer)).fork();
5477 +                }
5478 +                Map.Entry<K,V> r = null;
5479 +                for (Node<K,V> p; (p = advance()) != null; )
5480 +                    r = (r == null) ? p : reducer.apply(r, p);
5481 +                result = r;
5482 +                CountedCompleter<?> c;
5483 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5484 +                    @SuppressWarnings("unchecked")
5485 +                    ReduceEntriesTask<K,V>
5486 +                        t = (ReduceEntriesTask<K,V>)c,
5487 +                        s = t.rights;
5488 +                    while (s != null) {
5489 +                        Map.Entry<K,V> tr, sr;
5490 +                        if ((sr = s.result) != null)
5491 +                            t.result = (((tr = t.result) == null) ? sr :
5492 +                                        reducer.apply(tr, sr));
5493 +                        s = t.rights = s.nextRight;
5494 +                    }
5495 +                }
5496 +            }
5497 +        }
5498 +    }
5499 +
5500 +    @SuppressWarnings("serial")
5501 +    static final class MapReduceKeysTask<K,V,U>
5502 +        extends BulkTask<K,V,U> {
5503 +        final Function<? super K, ? extends U> transformer;
5504 +        final BiFunction<? super U, ? super U, ? extends U> reducer;
5505 +        U result;
5506 +        MapReduceKeysTask<K,V,U> rights, nextRight;
5507 +        MapReduceKeysTask
5508 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5509 +             MapReduceKeysTask<K,V,U> nextRight,
5510 +             Function<? super K, ? extends U> transformer,
5511 +             BiFunction<? super U, ? super U, ? extends U> reducer) {
5512 +            super(p, b, i, f, t); this.nextRight = nextRight;
5513 +            this.transformer = transformer;
5514 +            this.reducer = reducer;
5515 +        }
5516 +        public final U getRawResult() { return result; }
5517 +        public final void compute() {
5518 +            final Function<? super K, ? extends U> transformer;
5519 +            final BiFunction<? super U, ? super U, ? extends U> reducer;
5520 +            if ((transformer = this.transformer) != null &&
5521 +                (reducer = this.reducer) != null) {
5522 +                for (int i = baseIndex, f, h; batch > 0 &&
5523 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5524 +                    addToPendingCount(1);
5525 +                    (rights = new MapReduceKeysTask<K,V,U>
5526 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5527 +                      rights, transformer, reducer)).fork();
5528 +                }
5529 +                U r = null;
5530 +                for (Node<K,V> p; (p = advance()) != null; ) {
5531 +                    U u;
5532 +                    if ((u = transformer.apply(p.key)) != null)
5533 +                        r = (r == null) ? u : reducer.apply(r, u);
5534 +                }
5535 +                result = r;
5536 +                CountedCompleter<?> c;
5537 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5538 +                    @SuppressWarnings("unchecked")
5539 +                    MapReduceKeysTask<K,V,U>
5540 +                        t = (MapReduceKeysTask<K,V,U>)c,
5541 +                        s = t.rights;
5542 +                    while (s != null) {
5543 +                        U tr, sr;
5544 +                        if ((sr = s.result) != null)
5545 +                            t.result = (((tr = t.result) == null) ? sr :
5546 +                                        reducer.apply(tr, sr));
5547 +                        s = t.rights = s.nextRight;
5548 +                    }
5549 +                }
5550 +            }
5551 +        }
5552 +    }
5553 +
5554 +    @SuppressWarnings("serial")
5555 +    static final class MapReduceValuesTask<K,V,U>
5556 +        extends BulkTask<K,V,U> {
5557 +        final Function<? super V, ? extends U> transformer;
5558 +        final BiFunction<? super U, ? super U, ? extends U> reducer;
5559 +        U result;
5560 +        MapReduceValuesTask<K,V,U> rights, nextRight;
5561 +        MapReduceValuesTask
5562 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5563 +             MapReduceValuesTask<K,V,U> nextRight,
5564 +             Function<? super V, ? extends U> transformer,
5565 +             BiFunction<? super U, ? super U, ? extends U> reducer) {
5566 +            super(p, b, i, f, t); this.nextRight = nextRight;
5567 +            this.transformer = transformer;
5568 +            this.reducer = reducer;
5569 +        }
5570 +        public final U getRawResult() { return result; }
5571 +        public final void compute() {
5572 +            final Function<? super V, ? extends U> transformer;
5573 +            final BiFunction<? super U, ? super U, ? extends U> reducer;
5574 +            if ((transformer = this.transformer) != null &&
5575 +                (reducer = this.reducer) != null) {
5576 +                for (int i = baseIndex, f, h; batch > 0 &&
5577 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5578 +                    addToPendingCount(1);
5579 +                    (rights = new MapReduceValuesTask<K,V,U>
5580 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5581 +                      rights, transformer, reducer)).fork();
5582 +                }
5583 +                U r = null;
5584 +                for (Node<K,V> p; (p = advance()) != null; ) {
5585 +                    U u;
5586 +                    if ((u = transformer.apply(p.val)) != null)
5587 +                        r = (r == null) ? u : reducer.apply(r, u);
5588 +                }
5589 +                result = r;
5590 +                CountedCompleter<?> c;
5591 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5592 +                    @SuppressWarnings("unchecked")
5593 +                    MapReduceValuesTask<K,V,U>
5594 +                        t = (MapReduceValuesTask<K,V,U>)c,
5595 +                        s = t.rights;
5596 +                    while (s != null) {
5597 +                        U tr, sr;
5598 +                        if ((sr = s.result) != null)
5599 +                            t.result = (((tr = t.result) == null) ? sr :
5600 +                                        reducer.apply(tr, sr));
5601 +                        s = t.rights = s.nextRight;
5602 +                    }
5603 +                }
5604 +            }
5605 +        }
5606 +    }
5607 +
5608 +    @SuppressWarnings("serial")
5609 +    static final class MapReduceEntriesTask<K,V,U>
5610 +        extends BulkTask<K,V,U> {
5611 +        final Function<Map.Entry<K,V>, ? extends U> transformer;
5612 +        final BiFunction<? super U, ? super U, ? extends U> reducer;
5613 +        U result;
5614 +        MapReduceEntriesTask<K,V,U> rights, nextRight;
5615 +        MapReduceEntriesTask
5616 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5617 +             MapReduceEntriesTask<K,V,U> nextRight,
5618 +             Function<Map.Entry<K,V>, ? extends U> transformer,
5619 +             BiFunction<? super U, ? super U, ? extends U> reducer) {
5620 +            super(p, b, i, f, t); this.nextRight = nextRight;
5621 +            this.transformer = transformer;
5622 +            this.reducer = reducer;
5623 +        }
5624 +        public final U getRawResult() { return result; }
5625 +        public final void compute() {
5626 +            final Function<Map.Entry<K,V>, ? extends U> transformer;
5627 +            final BiFunction<? super U, ? super U, ? extends U> reducer;
5628 +            if ((transformer = this.transformer) != null &&
5629 +                (reducer = this.reducer) != null) {
5630 +                for (int i = baseIndex, f, h; batch > 0 &&
5631 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5632 +                    addToPendingCount(1);
5633 +                    (rights = new MapReduceEntriesTask<K,V,U>
5634 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5635 +                      rights, transformer, reducer)).fork();
5636 +                }
5637 +                U r = null;
5638 +                for (Node<K,V> p; (p = advance()) != null; ) {
5639 +                    U u;
5640 +                    if ((u = transformer.apply(p)) != null)
5641 +                        r = (r == null) ? u : reducer.apply(r, u);
5642 +                }
5643 +                result = r;
5644 +                CountedCompleter<?> c;
5645 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5646 +                    @SuppressWarnings("unchecked")
5647 +                    MapReduceEntriesTask<K,V,U>
5648 +                        t = (MapReduceEntriesTask<K,V,U>)c,
5649 +                        s = t.rights;
5650 +                    while (s != null) {
5651 +                        U tr, sr;
5652 +                        if ((sr = s.result) != null)
5653 +                            t.result = (((tr = t.result) == null) ? sr :
5654 +                                        reducer.apply(tr, sr));
5655 +                        s = t.rights = s.nextRight;
5656 +                    }
5657 +                }
5658 +            }
5659 +        }
5660 +    }
5661 +
5662 +    @SuppressWarnings("serial")
5663 +    static final class MapReduceMappingsTask<K,V,U>
5664 +        extends BulkTask<K,V,U> {
5665 +        final BiFunction<? super K, ? super V, ? extends U> transformer;
5666 +        final BiFunction<? super U, ? super U, ? extends U> reducer;
5667 +        U result;
5668 +        MapReduceMappingsTask<K,V,U> rights, nextRight;
5669 +        MapReduceMappingsTask
5670 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5671 +             MapReduceMappingsTask<K,V,U> nextRight,
5672 +             BiFunction<? super K, ? super V, ? extends U> transformer,
5673 +             BiFunction<? super U, ? super U, ? extends U> reducer) {
5674 +            super(p, b, i, f, t); this.nextRight = nextRight;
5675 +            this.transformer = transformer;
5676 +            this.reducer = reducer;
5677 +        }
5678 +        public final U getRawResult() { return result; }
5679 +        public final void compute() {
5680 +            final BiFunction<? super K, ? super V, ? extends U> transformer;
5681 +            final BiFunction<? super U, ? super U, ? extends U> reducer;
5682 +            if ((transformer = this.transformer) != null &&
5683 +                (reducer = this.reducer) != null) {
5684 +                for (int i = baseIndex, f, h; batch > 0 &&
5685 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5686 +                    addToPendingCount(1);
5687 +                    (rights = new MapReduceMappingsTask<K,V,U>
5688 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5689 +                      rights, transformer, reducer)).fork();
5690 +                }
5691 +                U r = null;
5692 +                for (Node<K,V> p; (p = advance()) != null; ) {
5693 +                    U u;
5694 +                    if ((u = transformer.apply(p.key, p.val)) != null)
5695 +                        r = (r == null) ? u : reducer.apply(r, u);
5696 +                }
5697 +                result = r;
5698 +                CountedCompleter<?> c;
5699 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5700 +                    @SuppressWarnings("unchecked")
5701 +                    MapReduceMappingsTask<K,V,U>
5702 +                        t = (MapReduceMappingsTask<K,V,U>)c,
5703 +                        s = t.rights;
5704 +                    while (s != null) {
5705 +                        U tr, sr;
5706 +                        if ((sr = s.result) != null)
5707 +                            t.result = (((tr = t.result) == null) ? sr :
5708 +                                        reducer.apply(tr, sr));
5709 +                        s = t.rights = s.nextRight;
5710 +                    }
5711 +                }
5712 +            }
5713 +        }
5714 +    }
5715 +
5716 +    @SuppressWarnings("serial")
5717 +    static final class MapReduceKeysToDoubleTask<K,V>
5718 +        extends BulkTask<K,V,Double> {
5719 +        final ToDoubleFunction<? super K> transformer;
5720 +        final DoubleBinaryOperator reducer;
5721 +        final double basis;
5722 +        double result;
5723 +        MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5724 +        MapReduceKeysToDoubleTask
5725 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5726 +             MapReduceKeysToDoubleTask<K,V> nextRight,
5727 +             ToDoubleFunction<? super K> transformer,
5728 +             double basis,
5729 +             DoubleBinaryOperator reducer) {
5730 +            super(p, b, i, f, t); this.nextRight = nextRight;
5731 +            this.transformer = transformer;
5732 +            this.basis = basis; this.reducer = reducer;
5733 +        }
5734 +        public final Double getRawResult() { return result; }
5735 +        public final void compute() {
5736 +            final ToDoubleFunction<? super K> transformer;
5737 +            final DoubleBinaryOperator reducer;
5738 +            if ((transformer = this.transformer) != null &&
5739 +                (reducer = this.reducer) != null) {
5740 +                double r = this.basis;
5741 +                for (int i = baseIndex, f, h; batch > 0 &&
5742 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5743 +                    addToPendingCount(1);
5744 +                    (rights = new MapReduceKeysToDoubleTask<K,V>
5745 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5746 +                      rights, transformer, r, reducer)).fork();
5747 +                }
5748 +                for (Node<K,V> p; (p = advance()) != null; )
5749 +                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
5750 +                result = r;
5751 +                CountedCompleter<?> c;
5752 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5753 +                    @SuppressWarnings("unchecked")
5754 +                    MapReduceKeysToDoubleTask<K,V>
5755 +                        t = (MapReduceKeysToDoubleTask<K,V>)c,
5756 +                        s = t.rights;
5757 +                    while (s != null) {
5758 +                        t.result = reducer.applyAsDouble(t.result, s.result);
5759 +                        s = t.rights = s.nextRight;
5760 +                    }
5761 +                }
5762 +            }
5763 +        }
5764 +    }
5765 +
5766 +    @SuppressWarnings("serial")
5767 +    static final class MapReduceValuesToDoubleTask<K,V>
5768 +        extends BulkTask<K,V,Double> {
5769 +        final ToDoubleFunction<? super V> transformer;
5770 +        final DoubleBinaryOperator reducer;
5771 +        final double basis;
5772 +        double result;
5773 +        MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5774 +        MapReduceValuesToDoubleTask
5775 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5776 +             MapReduceValuesToDoubleTask<K,V> nextRight,
5777 +             ToDoubleFunction<? super V> transformer,
5778 +             double basis,
5779 +             DoubleBinaryOperator reducer) {
5780 +            super(p, b, i, f, t); this.nextRight = nextRight;
5781 +            this.transformer = transformer;
5782 +            this.basis = basis; this.reducer = reducer;
5783 +        }
5784 +        public final Double getRawResult() { return result; }
5785 +        public final void compute() {
5786 +            final ToDoubleFunction<? super V> transformer;
5787 +            final DoubleBinaryOperator reducer;
5788 +            if ((transformer = this.transformer) != null &&
5789 +                (reducer = this.reducer) != null) {
5790 +                double r = this.basis;
5791 +                for (int i = baseIndex, f, h; batch > 0 &&
5792 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5793 +                    addToPendingCount(1);
5794 +                    (rights = new MapReduceValuesToDoubleTask<K,V>
5795 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5796 +                      rights, transformer, r, reducer)).fork();
5797 +                }
5798 +                for (Node<K,V> p; (p = advance()) != null; )
5799 +                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
5800 +                result = r;
5801 +                CountedCompleter<?> c;
5802 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5803 +                    @SuppressWarnings("unchecked")
5804 +                    MapReduceValuesToDoubleTask<K,V>
5805 +                        t = (MapReduceValuesToDoubleTask<K,V>)c,
5806 +                        s = t.rights;
5807 +                    while (s != null) {
5808 +                        t.result = reducer.applyAsDouble(t.result, s.result);
5809 +                        s = t.rights = s.nextRight;
5810 +                    }
5811 +                }
5812 +            }
5813 +        }
5814 +    }
5815 +
5816 +    @SuppressWarnings("serial")
5817 +    static final class MapReduceEntriesToDoubleTask<K,V>
5818 +        extends BulkTask<K,V,Double> {
5819 +        final ToDoubleFunction<Map.Entry<K,V>> transformer;
5820 +        final DoubleBinaryOperator reducer;
5821 +        final double basis;
5822 +        double result;
5823 +        MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5824 +        MapReduceEntriesToDoubleTask
5825 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5826 +             MapReduceEntriesToDoubleTask<K,V> nextRight,
5827 +             ToDoubleFunction<Map.Entry<K,V>> transformer,
5828 +             double basis,
5829 +             DoubleBinaryOperator reducer) {
5830 +            super(p, b, i, f, t); this.nextRight = nextRight;
5831 +            this.transformer = transformer;
5832 +            this.basis = basis; this.reducer = reducer;
5833 +        }
5834 +        public final Double getRawResult() { return result; }
5835 +        public final void compute() {
5836 +            final ToDoubleFunction<Map.Entry<K,V>> transformer;
5837 +            final DoubleBinaryOperator reducer;
5838 +            if ((transformer = this.transformer) != null &&
5839 +                (reducer = this.reducer) != null) {
5840 +                double r = this.basis;
5841 +                for (int i = baseIndex, f, h; batch > 0 &&
5842 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5843 +                    addToPendingCount(1);
5844 +                    (rights = new MapReduceEntriesToDoubleTask<K,V>
5845 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5846 +                      rights, transformer, r, reducer)).fork();
5847 +                }
5848 +                for (Node<K,V> p; (p = advance()) != null; )
5849 +                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
5850 +                result = r;
5851 +                CountedCompleter<?> c;
5852 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5853 +                    @SuppressWarnings("unchecked")
5854 +                    MapReduceEntriesToDoubleTask<K,V>
5855 +                        t = (MapReduceEntriesToDoubleTask<K,V>)c,
5856 +                        s = t.rights;
5857 +                    while (s != null) {
5858 +                        t.result = reducer.applyAsDouble(t.result, s.result);
5859 +                        s = t.rights = s.nextRight;
5860 +                    }
5861 +                }
5862 +            }
5863 +        }
5864 +    }
5865 +
5866 +    @SuppressWarnings("serial")
5867 +    static final class MapReduceMappingsToDoubleTask<K,V>
5868 +        extends BulkTask<K,V,Double> {
5869 +        final ToDoubleBiFunction<? super K, ? super V> transformer;
5870 +        final DoubleBinaryOperator reducer;
5871 +        final double basis;
5872 +        double result;
5873 +        MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5874 +        MapReduceMappingsToDoubleTask
5875 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5876 +             MapReduceMappingsToDoubleTask<K,V> nextRight,
5877 +             ToDoubleBiFunction<? super K, ? super V> transformer,
5878 +             double basis,
5879 +             DoubleBinaryOperator reducer) {
5880 +            super(p, b, i, f, t); this.nextRight = nextRight;
5881 +            this.transformer = transformer;
5882 +            this.basis = basis; this.reducer = reducer;
5883 +        }
5884 +        public final Double getRawResult() { return result; }
5885 +        public final void compute() {
5886 +            final ToDoubleBiFunction<? super K, ? super V> transformer;
5887 +            final DoubleBinaryOperator reducer;
5888 +            if ((transformer = this.transformer) != null &&
5889 +                (reducer = this.reducer) != null) {
5890 +                double r = this.basis;
5891 +                for (int i = baseIndex, f, h; batch > 0 &&
5892 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5893 +                    addToPendingCount(1);
5894 +                    (rights = new MapReduceMappingsToDoubleTask<K,V>
5895 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5896 +                      rights, transformer, r, reducer)).fork();
5897 +                }
5898 +                for (Node<K,V> p; (p = advance()) != null; )
5899 +                    r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
5900 +                result = r;
5901 +                CountedCompleter<?> c;
5902 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5903 +                    @SuppressWarnings("unchecked")
5904 +                    MapReduceMappingsToDoubleTask<K,V>
5905 +                        t = (MapReduceMappingsToDoubleTask<K,V>)c,
5906 +                        s = t.rights;
5907 +                    while (s != null) {
5908 +                        t.result = reducer.applyAsDouble(t.result, s.result);
5909 +                        s = t.rights = s.nextRight;
5910 +                    }
5911 +                }
5912 +            }
5913 +        }
5914 +    }
5915 +
5916 +    @SuppressWarnings("serial")
5917 +    static final class MapReduceKeysToLongTask<K,V>
5918 +        extends BulkTask<K,V,Long> {
5919 +        final ToLongFunction<? super K> transformer;
5920 +        final LongBinaryOperator reducer;
5921 +        final long basis;
5922 +        long result;
5923 +        MapReduceKeysToLongTask<K,V> rights, nextRight;
5924 +        MapReduceKeysToLongTask
5925 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5926 +             MapReduceKeysToLongTask<K,V> nextRight,
5927 +             ToLongFunction<? super K> transformer,
5928 +             long basis,
5929 +             LongBinaryOperator reducer) {
5930 +            super(p, b, i, f, t); this.nextRight = nextRight;
5931 +            this.transformer = transformer;
5932 +            this.basis = basis; this.reducer = reducer;
5933 +        }
5934 +        public final Long getRawResult() { return result; }
5935 +        public final void compute() {
5936 +            final ToLongFunction<? super K> transformer;
5937 +            final LongBinaryOperator reducer;
5938 +            if ((transformer = this.transformer) != null &&
5939 +                (reducer = this.reducer) != null) {
5940 +                long r = this.basis;
5941 +                for (int i = baseIndex, f, h; batch > 0 &&
5942 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5943 +                    addToPendingCount(1);
5944 +                    (rights = new MapReduceKeysToLongTask<K,V>
5945 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5946 +                      rights, transformer, r, reducer)).fork();
5947 +                }
5948 +                for (Node<K,V> p; (p = advance()) != null; )
5949 +                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key));
5950 +                result = r;
5951 +                CountedCompleter<?> c;
5952 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
5953 +                    @SuppressWarnings("unchecked")
5954 +                    MapReduceKeysToLongTask<K,V>
5955 +                        t = (MapReduceKeysToLongTask<K,V>)c,
5956 +                        s = t.rights;
5957 +                    while (s != null) {
5958 +                        t.result = reducer.applyAsLong(t.result, s.result);
5959 +                        s = t.rights = s.nextRight;
5960 +                    }
5961 +                }
5962 +            }
5963 +        }
5964 +    }
5965 +
5966 +    @SuppressWarnings("serial")
5967 +    static final class MapReduceValuesToLongTask<K,V>
5968 +        extends BulkTask<K,V,Long> {
5969 +        final ToLongFunction<? super V> transformer;
5970 +        final LongBinaryOperator reducer;
5971 +        final long basis;
5972 +        long result;
5973 +        MapReduceValuesToLongTask<K,V> rights, nextRight;
5974 +        MapReduceValuesToLongTask
5975 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5976 +             MapReduceValuesToLongTask<K,V> nextRight,
5977 +             ToLongFunction<? super V> transformer,
5978 +             long basis,
5979 +             LongBinaryOperator reducer) {
5980 +            super(p, b, i, f, t); this.nextRight = nextRight;
5981 +            this.transformer = transformer;
5982 +            this.basis = basis; this.reducer = reducer;
5983 +        }
5984 +        public final Long getRawResult() { return result; }
5985 +        public final void compute() {
5986 +            final ToLongFunction<? super V> transformer;
5987 +            final LongBinaryOperator reducer;
5988 +            if ((transformer = this.transformer) != null &&
5989 +                (reducer = this.reducer) != null) {
5990 +                long r = this.basis;
5991 +                for (int i = baseIndex, f, h; batch > 0 &&
5992 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
5993 +                    addToPendingCount(1);
5994 +                    (rights = new MapReduceValuesToLongTask<K,V>
5995 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
5996 +                      rights, transformer, r, reducer)).fork();
5997 +                }
5998 +                for (Node<K,V> p; (p = advance()) != null; )
5999 +                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.val));
6000 +                result = r;
6001 +                CountedCompleter<?> c;
6002 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6003 +                    @SuppressWarnings("unchecked")
6004 +                    MapReduceValuesToLongTask<K,V>
6005 +                        t = (MapReduceValuesToLongTask<K,V>)c,
6006 +                        s = t.rights;
6007 +                    while (s != null) {
6008 +                        t.result = reducer.applyAsLong(t.result, s.result);
6009 +                        s = t.rights = s.nextRight;
6010 +                    }
6011 +                }
6012 +            }
6013 +        }
6014 +    }
6015 +
6016 +    @SuppressWarnings("serial")
6017 +    static final class MapReduceEntriesToLongTask<K,V>
6018 +        extends BulkTask<K,V,Long> {
6019 +        final ToLongFunction<Map.Entry<K,V>> transformer;
6020 +        final LongBinaryOperator reducer;
6021 +        final long basis;
6022 +        long result;
6023 +        MapReduceEntriesToLongTask<K,V> rights, nextRight;
6024 +        MapReduceEntriesToLongTask
6025 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6026 +             MapReduceEntriesToLongTask<K,V> nextRight,
6027 +             ToLongFunction<Map.Entry<K,V>> transformer,
6028 +             long basis,
6029 +             LongBinaryOperator reducer) {
6030 +            super(p, b, i, f, t); this.nextRight = nextRight;
6031 +            this.transformer = transformer;
6032 +            this.basis = basis; this.reducer = reducer;
6033 +        }
6034 +        public final Long getRawResult() { return result; }
6035 +        public final void compute() {
6036 +            final ToLongFunction<Map.Entry<K,V>> transformer;
6037 +            final LongBinaryOperator reducer;
6038 +            if ((transformer = this.transformer) != null &&
6039 +                (reducer = this.reducer) != null) {
6040 +                long r = this.basis;
6041 +                for (int i = baseIndex, f, h; batch > 0 &&
6042 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6043 +                    addToPendingCount(1);
6044 +                    (rights = new MapReduceEntriesToLongTask<K,V>
6045 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6046 +                      rights, transformer, r, reducer)).fork();
6047 +                }
6048 +                for (Node<K,V> p; (p = advance()) != null; )
6049 +                    r = reducer.applyAsLong(r, transformer.applyAsLong(p));
6050 +                result = r;
6051 +                CountedCompleter<?> c;
6052 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6053 +                    @SuppressWarnings("unchecked")
6054 +                    MapReduceEntriesToLongTask<K,V>
6055 +                        t = (MapReduceEntriesToLongTask<K,V>)c,
6056 +                        s = t.rights;
6057 +                    while (s != null) {
6058 +                        t.result = reducer.applyAsLong(t.result, s.result);
6059 +                        s = t.rights = s.nextRight;
6060 +                    }
6061 +                }
6062 +            }
6063 +        }
6064 +    }
6065 +
6066 +    @SuppressWarnings("serial")
6067 +    static final class MapReduceMappingsToLongTask<K,V>
6068 +        extends BulkTask<K,V,Long> {
6069 +        final ToLongBiFunction<? super K, ? super V> transformer;
6070 +        final LongBinaryOperator reducer;
6071 +        final long basis;
6072 +        long result;
6073 +        MapReduceMappingsToLongTask<K,V> rights, nextRight;
6074 +        MapReduceMappingsToLongTask
6075 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6076 +             MapReduceMappingsToLongTask<K,V> nextRight,
6077 +             ToLongBiFunction<? super K, ? super V> transformer,
6078 +             long basis,
6079 +             LongBinaryOperator reducer) {
6080 +            super(p, b, i, f, t); this.nextRight = nextRight;
6081 +            this.transformer = transformer;
6082 +            this.basis = basis; this.reducer = reducer;
6083 +        }
6084 +        public final Long getRawResult() { return result; }
6085 +        public final void compute() {
6086 +            final ToLongBiFunction<? super K, ? super V> transformer;
6087 +            final LongBinaryOperator reducer;
6088 +            if ((transformer = this.transformer) != null &&
6089 +                (reducer = this.reducer) != null) {
6090 +                long r = this.basis;
6091 +                for (int i = baseIndex, f, h; batch > 0 &&
6092 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6093 +                    addToPendingCount(1);
6094 +                    (rights = new MapReduceMappingsToLongTask<K,V>
6095 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6096 +                      rights, transformer, r, reducer)).fork();
6097 +                }
6098 +                for (Node<K,V> p; (p = advance()) != null; )
6099 +                    r = reducer.applyAsLong(r, transformer.applyAsLong(p.key, p.val));
6100 +                result = r;
6101 +                CountedCompleter<?> c;
6102 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6103 +                    @SuppressWarnings("unchecked")
6104 +                    MapReduceMappingsToLongTask<K,V>
6105 +                        t = (MapReduceMappingsToLongTask<K,V>)c,
6106 +                        s = t.rights;
6107 +                    while (s != null) {
6108 +                        t.result = reducer.applyAsLong(t.result, s.result);
6109 +                        s = t.rights = s.nextRight;
6110 +                    }
6111 +                }
6112 +            }
6113 +        }
6114 +    }
6115 +
6116 +    @SuppressWarnings("serial")
6117 +    static final class MapReduceKeysToIntTask<K,V>
6118 +        extends BulkTask<K,V,Integer> {
6119 +        final ToIntFunction<? super K> transformer;
6120 +        final IntBinaryOperator reducer;
6121 +        final int basis;
6122 +        int result;
6123 +        MapReduceKeysToIntTask<K,V> rights, nextRight;
6124 +        MapReduceKeysToIntTask
6125 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6126 +             MapReduceKeysToIntTask<K,V> nextRight,
6127 +             ToIntFunction<? super K> transformer,
6128 +             int basis,
6129 +             IntBinaryOperator reducer) {
6130 +            super(p, b, i, f, t); this.nextRight = nextRight;
6131 +            this.transformer = transformer;
6132 +            this.basis = basis; this.reducer = reducer;
6133 +        }
6134 +        public final Integer getRawResult() { return result; }
6135 +        public final void compute() {
6136 +            final ToIntFunction<? super K> transformer;
6137 +            final IntBinaryOperator reducer;
6138 +            if ((transformer = this.transformer) != null &&
6139 +                (reducer = this.reducer) != null) {
6140 +                int r = this.basis;
6141 +                for (int i = baseIndex, f, h; batch > 0 &&
6142 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6143 +                    addToPendingCount(1);
6144 +                    (rights = new MapReduceKeysToIntTask<K,V>
6145 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6146 +                      rights, transformer, r, reducer)).fork();
6147 +                }
6148 +                for (Node<K,V> p; (p = advance()) != null; )
6149 +                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key));
6150 +                result = r;
6151 +                CountedCompleter<?> c;
6152 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6153 +                    @SuppressWarnings("unchecked")
6154 +                    MapReduceKeysToIntTask<K,V>
6155 +                        t = (MapReduceKeysToIntTask<K,V>)c,
6156 +                        s = t.rights;
6157 +                    while (s != null) {
6158 +                        t.result = reducer.applyAsInt(t.result, s.result);
6159 +                        s = t.rights = s.nextRight;
6160 +                    }
6161 +                }
6162 +            }
6163 +        }
6164 +    }
6165 +
6166 +    @SuppressWarnings("serial")
6167 +    static final class MapReduceValuesToIntTask<K,V>
6168 +        extends BulkTask<K,V,Integer> {
6169 +        final ToIntFunction<? super V> transformer;
6170 +        final IntBinaryOperator reducer;
6171 +        final int basis;
6172 +        int result;
6173 +        MapReduceValuesToIntTask<K,V> rights, nextRight;
6174 +        MapReduceValuesToIntTask
6175 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6176 +             MapReduceValuesToIntTask<K,V> nextRight,
6177 +             ToIntFunction<? super V> transformer,
6178 +             int basis,
6179 +             IntBinaryOperator reducer) {
6180 +            super(p, b, i, f, t); this.nextRight = nextRight;
6181 +            this.transformer = transformer;
6182 +            this.basis = basis; this.reducer = reducer;
6183 +        }
6184 +        public final Integer getRawResult() { return result; }
6185 +        public final void compute() {
6186 +            final ToIntFunction<? super V> transformer;
6187 +            final IntBinaryOperator reducer;
6188 +            if ((transformer = this.transformer) != null &&
6189 +                (reducer = this.reducer) != null) {
6190 +                int r = this.basis;
6191 +                for (int i = baseIndex, f, h; batch > 0 &&
6192 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6193 +                    addToPendingCount(1);
6194 +                    (rights = new MapReduceValuesToIntTask<K,V>
6195 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6196 +                      rights, transformer, r, reducer)).fork();
6197 +                }
6198 +                for (Node<K,V> p; (p = advance()) != null; )
6199 +                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.val));
6200 +                result = r;
6201 +                CountedCompleter<?> c;
6202 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6203 +                    @SuppressWarnings("unchecked")
6204 +                    MapReduceValuesToIntTask<K,V>
6205 +                        t = (MapReduceValuesToIntTask<K,V>)c,
6206 +                        s = t.rights;
6207 +                    while (s != null) {
6208 +                        t.result = reducer.applyAsInt(t.result, s.result);
6209 +                        s = t.rights = s.nextRight;
6210 +                    }
6211 +                }
6212 +            }
6213 +        }
6214 +    }
6215 +
6216 +    @SuppressWarnings("serial")
6217 +    static final class MapReduceEntriesToIntTask<K,V>
6218 +        extends BulkTask<K,V,Integer> {
6219 +        final ToIntFunction<Map.Entry<K,V>> transformer;
6220 +        final IntBinaryOperator reducer;
6221 +        final int basis;
6222 +        int result;
6223 +        MapReduceEntriesToIntTask<K,V> rights, nextRight;
6224 +        MapReduceEntriesToIntTask
6225 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6226 +             MapReduceEntriesToIntTask<K,V> nextRight,
6227 +             ToIntFunction<Map.Entry<K,V>> transformer,
6228 +             int basis,
6229 +             IntBinaryOperator reducer) {
6230 +            super(p, b, i, f, t); this.nextRight = nextRight;
6231 +            this.transformer = transformer;
6232 +            this.basis = basis; this.reducer = reducer;
6233 +        }
6234 +        public final Integer getRawResult() { return result; }
6235 +        public final void compute() {
6236 +            final ToIntFunction<Map.Entry<K,V>> transformer;
6237 +            final IntBinaryOperator reducer;
6238 +            if ((transformer = this.transformer) != null &&
6239 +                (reducer = this.reducer) != null) {
6240 +                int r = this.basis;
6241 +                for (int i = baseIndex, f, h; batch > 0 &&
6242 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6243 +                    addToPendingCount(1);
6244 +                    (rights = new MapReduceEntriesToIntTask<K,V>
6245 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6246 +                      rights, transformer, r, reducer)).fork();
6247 +                }
6248 +                for (Node<K,V> p; (p = advance()) != null; )
6249 +                    r = reducer.applyAsInt(r, transformer.applyAsInt(p));
6250 +                result = r;
6251 +                CountedCompleter<?> c;
6252 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6253 +                    @SuppressWarnings("unchecked")
6254 +                    MapReduceEntriesToIntTask<K,V>
6255 +                        t = (MapReduceEntriesToIntTask<K,V>)c,
6256 +                        s = t.rights;
6257 +                    while (s != null) {
6258 +                        t.result = reducer.applyAsInt(t.result, s.result);
6259 +                        s = t.rights = s.nextRight;
6260 +                    }
6261 +                }
6262 +            }
6263 +        }
6264 +    }
6265 +
6266 +    @SuppressWarnings("serial")
6267 +    static final class MapReduceMappingsToIntTask<K,V>
6268 +        extends BulkTask<K,V,Integer> {
6269 +        final ToIntBiFunction<? super K, ? super V> transformer;
6270 +        final IntBinaryOperator reducer;
6271 +        final int basis;
6272 +        int result;
6273 +        MapReduceMappingsToIntTask<K,V> rights, nextRight;
6274 +        MapReduceMappingsToIntTask
6275 +            (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6276 +             MapReduceMappingsToIntTask<K,V> nextRight,
6277 +             ToIntBiFunction<? super K, ? super V> transformer,
6278 +             int basis,
6279 +             IntBinaryOperator reducer) {
6280 +            super(p, b, i, f, t); this.nextRight = nextRight;
6281 +            this.transformer = transformer;
6282 +            this.basis = basis; this.reducer = reducer;
6283 +        }
6284 +        public final Integer getRawResult() { return result; }
6285 +        public final void compute() {
6286 +            final ToIntBiFunction<? super K, ? super V> transformer;
6287 +            final IntBinaryOperator reducer;
6288 +            if ((transformer = this.transformer) != null &&
6289 +                (reducer = this.reducer) != null) {
6290 +                int r = this.basis;
6291 +                for (int i = baseIndex, f, h; batch > 0 &&
6292 +                         (h = ((f = baseLimit) + i) >>> 1) > i;) {
6293 +                    addToPendingCount(1);
6294 +                    (rights = new MapReduceMappingsToIntTask<K,V>
6295 +                     (this, batch >>>= 1, baseLimit = h, f, tab,
6296 +                      rights, transformer, r, reducer)).fork();
6297 +                }
6298 +                for (Node<K,V> p; (p = advance()) != null; )
6299 +                    r = reducer.applyAsInt(r, transformer.applyAsInt(p.key, p.val));
6300 +                result = r;
6301 +                CountedCompleter<?> c;
6302 +                for (c = firstComplete(); c != null; c = c.nextComplete()) {
6303 +                    @SuppressWarnings("unchecked")
6304 +                    MapReduceMappingsToIntTask<K,V>
6305 +                        t = (MapReduceMappingsToIntTask<K,V>)c,
6306 +                        s = t.rights;
6307 +                    while (s != null) {
6308 +                        t.result = reducer.applyAsInt(t.result, s.result);
6309 +                        s = t.rights = s.nextRight;
6310 +                    }
6311 +                }
6312 +            }
6313 +        }
6314 +    }
6315 +
6316 +    // Unsafe mechanics
6317 +    private static final Unsafe U = Unsafe.getUnsafe();
6318 +    private static final long SIZECTL
6319 +        = U.objectFieldOffset(ConcurrentHashMap.class, "sizeCtl");
6320 +    private static final long TRANSFERINDEX
6321 +        = U.objectFieldOffset(ConcurrentHashMap.class, "transferIndex");
6322 +    private static final long BASECOUNT
6323 +        = U.objectFieldOffset(ConcurrentHashMap.class, "baseCount");
6324 +    private static final long CELLSBUSY
6325 +        = U.objectFieldOffset(ConcurrentHashMap.class, "cellsBusy");
6326 +    private static final long CELLVALUE
6327 +        = U.objectFieldOffset(CounterCell.class, "value");
6328 +    private static final int ABASE = U.arrayBaseOffset(Node[].class);
6329 +    private static final int ASHIFT;
6330 +
6331 +    static {
6332 +        int scale = U.arrayIndexScale(Node[].class);
6333 +        if ((scale & (scale - 1)) != 0)
6334 +            throw new ExceptionInInitializerError("array index scale not a power of two");
6335 +        ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6336 +
6337 +        // Reduce the risk of rare disastrous classloading in first call to
6338 +        // LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773
6339 +        Class<?> ensureLoaded = LockSupport.class;
6340 +
6341 +        // Eager class load observed to help JIT during startup
6342 +        ensureLoaded = ReservationNode.class;
6343 +    }
6344   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines