src/java.base/share/classes/java/util/LinkedHashMap.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55391
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    27
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    28
import java.util.function.Consumer;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18156
diff changeset
    29
import java.util.function.BiConsumer;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18156
diff changeset
    30
import java.util.function.BiFunction;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    31
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    34
 * <p>Hash table and linked list implementation of the {@code Map} interface,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * with predictable iteration order.  This implementation differs from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    36
 * {@code HashMap} in that it maintains a doubly-linked list running through
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * all of its entries.  This linked list defines the iteration ordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which is normally the order in which keys were inserted into the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * (<i>insertion-order</i>).  Note that insertion order is not affected
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    40
 * if a key is <i>re-inserted</i> into the map.  (A key {@code k} is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    41
 * reinserted into a map {@code m} if {@code m.put(k, v)} is invoked when
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    42
 * {@code m.containsKey(k)} would return {@code true} immediately prior to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * the invocation.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>This implementation spares its clients from the unspecified, generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * chaotic ordering provided by {@link HashMap} (and {@link Hashtable}),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * without incurring the increased cost associated with {@link TreeMap}.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * can be used to produce a copy of a map that has the same order as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * original, regardless of the original map's implementation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     void foo(Map m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *         Map copy = new LinkedHashMap(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This technique is particularly useful if a module takes a map on input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * copies it, and later returns results whose order is determined by that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the copy.  (Clients generally appreciate having things returned in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * order they were presented.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>A special {@link #LinkedHashMap(int,float,boolean) constructor} is
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    62
 * provided to create a linked hash map whose order of iteration is the order
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    63
 * in which its entries were last accessed, from least-recently accessed to
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
    64
 * most-recently (<i>access-order</i>).  This kind of map is well-suited to
22058
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    65
 * building LRU caches.  Invoking the {@code put}, {@code putIfAbsent},
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    66
 * {@code get}, {@code getOrDefault}, {@code compute}, {@code computeIfAbsent},
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    67
 * {@code computeIfPresent}, or {@code merge} methods results
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    68
 * in an access to the corresponding entry (assuming it exists after the
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    69
 * invocation completes). The {@code replace} methods only result in an access
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    70
 * of the entry if the value is replaced.  The {@code putAll} method generates one
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    71
 * entry access for each mapping in the specified map, in the order that
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    72
 * key-value mappings are provided by the specified map's entry set iterator.
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    73
 * <i>No other methods generate entry accesses.</i>  In particular, operations
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    74
 * on collection-views do <i>not</i> affect the order of iteration of the
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
    75
 * backing map.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * impose a policy for removing stale mappings automatically when new mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * are added to the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    81
 * <p>This class provides all of the optional {@code Map} operations, and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    82
 * permits null elements.  Like {@code HashMap}, it provides constant-time
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    83
 * performance for the basic operations ({@code add}, {@code contains} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    84
 * {@code remove}), assuming the hash function disperses elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * properly among the buckets.  Performance is likely to be just slightly
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    86
 * below that of {@code HashMap}, due to the added expense of maintaining the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * linked list, with one exception: Iteration over the collection-views
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    88
 * of a {@code LinkedHashMap} requires time proportional to the <i>size</i>
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    89
 * of the map, regardless of its capacity.  Iteration over a {@code HashMap}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * is likely to be more expensive, requiring time proportional to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <i>capacity</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>A linked hash map has two parameters that affect its performance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <i>initial capacity</i> and <i>load factor</i>.  They are defined precisely
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    95
 * as for {@code HashMap}.  Note, however, that the penalty for choosing an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * excessively high value for initial capacity is less severe for this class
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    97
 * than for {@code HashMap}, as iteration times for this class are unaffected
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * by capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * If multiple threads access a linked hash map concurrently, and at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * one of the threads modifies the map structurally, it <em>must</em> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * synchronized externally.  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *   Map m = Collections.synchronizedMap(new LinkedHashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * A structural modification is any operation that adds or deletes one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * mappings or, in the case of access-ordered linked hash maps, affects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * iteration order.  In insertion-ordered linked hash maps, merely changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * the value associated with a key that is already contained in the map is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * a structural modification.  <strong>In access-ordered linked hash maps,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   117
 * merely querying the map with {@code get} is a structural modification.
22058
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   118
 * </strong>)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   120
 * <p>The iterators returned by the {@code iterator} method of the collections
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * returned by all of this class's collection view methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <em>fail-fast</em>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * the iterator is created, in any way except through the iterator's own
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   124
 * {@code remove} method, the iterator will throw a {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * arbitrary, non-deterministic behavior at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   132
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * exception for its correctness:   <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   137
 * <p>The spliterators returned by the spliterator method of the collections
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   138
 * returned by all of this class's collection view methods are
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   139
 * <em><a href="Spliterator.html#binding">late-binding</a></em>,
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   140
 * <em>fail-fast</em>, and additionally report {@link Spliterator#ORDERED}.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   141
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47216
diff changeset
   143
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
19435
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   146
 * @implNote
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   147
 * The spliterators returned by the spliterator method of the collections
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   148
 * returned by all of this class's collection view methods are created from
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   149
 * the iterators of the corresponding collections.
9d7530ff42cb 8014824: Document Spliterator characteristics and binding policy of java util collection impls
psandoz
parents: 18280
diff changeset
   150
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
public class LinkedHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    extends HashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    implements Map<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   168
    /*
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   169
     * Implementation note.  A previous version of this class was
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   170
     * internally structured a little differently. Because superclass
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   171
     * HashMap now uses trees for some of its nodes, class
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   172
     * LinkedHashMap.Entry is now treated as intermediary node class
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   173
     * that can also be converted to tree form. The name of this
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   174
     * class, LinkedHashMap.Entry, is confusing in several ways in its
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   175
     * current context, but cannot be changed.  Otherwise, even though
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   176
     * it is not exported outside this package, some existing source
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   177
     * code is known to have relied on a symbol resolution corner case
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   178
     * rule in calls to removeEldestEntry that suppressed compilation
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   179
     * errors due to ambiguous usages. So, we keep the name to
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   180
     * preserve unmodified compilability.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   181
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   182
     * The changes in node classes also require using two fields
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   183
     * (head, tail) rather than a pointer to a header node to maintain
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   184
     * the doubly-linked before/after list. This class also
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   185
     * previously used a different style of callback methods upon
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   186
     * access, insertion, and removal.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   187
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   188
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   189
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   190
     * HashMap.Node subclass for normal LinkedHashMap entries.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   191
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   192
    static class Entry<K,V> extends HashMap.Node<K,V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   193
        Entry<K,V> before, after;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   194
        Entry(int hash, K key, V value, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   195
            super(hash, key, value, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   196
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   197
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   198
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55391
diff changeset
   199
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static final long serialVersionUID = 3801124242820219131L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   203
     * The head (eldest) of the doubly linked list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   205
    transient LinkedHashMap.Entry<K,V> head;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   206
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   207
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   208
     * The tail (youngest) of the doubly linked list.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   209
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   210
    transient LinkedHashMap.Entry<K,V> tail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   213
     * The iteration ordering method for this linked hash map: {@code true}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   214
     * for access-order, {@code false} for insertion-order.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   218
    final boolean accessOrder;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   219
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   220
    // internal utilities
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   221
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   222
    // link at the end of list
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   223
    private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   224
        LinkedHashMap.Entry<K,V> last = tail;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   225
        tail = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   226
        if (last == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   227
            head = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   228
        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   229
            p.before = last;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   230
            last.after = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   231
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   232
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   233
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   234
    // apply src's links to dst
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   235
    private void transferLinks(LinkedHashMap.Entry<K,V> src,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   236
                               LinkedHashMap.Entry<K,V> dst) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   237
        LinkedHashMap.Entry<K,V> b = dst.before = src.before;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   238
        LinkedHashMap.Entry<K,V> a = dst.after = src.after;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   239
        if (b == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   240
            head = dst;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   241
        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   242
            b.after = dst;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   243
        if (a == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   244
            tail = dst;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   245
        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   246
            a.before = dst;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   247
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   248
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   249
    // overrides of HashMap hook methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   250
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   251
    void reinitialize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   252
        super.reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   253
        head = tail = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   254
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   255
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   256
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   257
        LinkedHashMap.Entry<K,V> p =
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22058
diff changeset
   258
            new LinkedHashMap.Entry<>(hash, key, value, e);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   259
        linkNodeLast(p);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   260
        return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   261
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   262
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   263
    Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   264
        LinkedHashMap.Entry<K,V> q = (LinkedHashMap.Entry<K,V>)p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   265
        LinkedHashMap.Entry<K,V> t =
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22058
diff changeset
   266
            new LinkedHashMap.Entry<>(q.hash, q.key, q.value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   267
        transferLinks(q, t);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   268
        return t;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   269
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   270
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   271
    TreeNode<K,V> newTreeNode(int hash, K key, V value, Node<K,V> next) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22058
diff changeset
   272
        TreeNode<K,V> p = new TreeNode<>(hash, key, value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   273
        linkNodeLast(p);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   274
        return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   275
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   276
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   277
    TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   278
        LinkedHashMap.Entry<K,V> q = (LinkedHashMap.Entry<K,V>)p;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22058
diff changeset
   279
        TreeNode<K,V> t = new TreeNode<>(q.hash, q.key, q.value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   280
        transferLinks(q, t);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   281
        return t;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   282
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   283
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   284
    void afterNodeRemoval(Node<K,V> e) { // unlink
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   285
        LinkedHashMap.Entry<K,V> p =
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   286
            (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   287
        p.before = p.after = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   288
        if (b == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   289
            head = a;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   290
        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   291
            b.after = a;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   292
        if (a == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   293
            tail = b;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   294
        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   295
            a.before = b;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   296
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   297
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   298
    void afterNodeInsertion(boolean evict) { // possibly remove eldest
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   299
        LinkedHashMap.Entry<K,V> first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   300
        if (evict && (first = head) != null && removeEldestEntry(first)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   301
            K key = first.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   302
            removeNode(hash(key), key, null, false, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   303
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   304
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   305
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   306
    void afterNodeAccess(Node<K,V> e) { // move node to last
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   307
        LinkedHashMap.Entry<K,V> last;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   308
        if (accessOrder && (last = tail) != e) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   309
            LinkedHashMap.Entry<K,V> p =
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   310
                (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   311
            p.after = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   312
            if (b == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   313
                head = a;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   314
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   315
                b.after = a;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   316
            if (a != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   317
                a.before = b;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   318
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   319
                last = b;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   320
            if (last == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   321
                head = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   322
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   323
                p.before = last;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   324
                last.after = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   325
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   326
            tail = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   327
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   328
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   329
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   330
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   331
    void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   332
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   333
            s.writeObject(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   334
            s.writeObject(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   335
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   336
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   339
     * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * with the specified initial capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public LinkedHashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        super(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   353
     * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * with the specified initial capacity and a default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public LinkedHashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        super(initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   365
     * Constructs an empty insertion-ordered {@code LinkedHashMap} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * with the default initial capacity (16) and load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public LinkedHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        accessOrder = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   374
     * Constructs an insertion-ordered {@code LinkedHashMap} instance with
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   375
     * the same mappings as the specified map.  The {@code LinkedHashMap}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * instance is created with a default load factor (0.75) and an initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * capacity sufficient to hold the mappings in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @param  m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    public LinkedHashMap(Map<? extends K, ? extends V> m) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   383
        super();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        accessOrder = false;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   385
        putMapEntries(m, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   389
     * Constructs an empty {@code LinkedHashMap} instance with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * specified initial capacity, load factor and ordering mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param  loadFactor      the load factor
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   394
     * @param  accessOrder     the ordering mode - {@code true} for
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   395
     *         access-order, {@code false} for insertion-order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public LinkedHashMap(int initialCapacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                         float loadFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                         boolean accessOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        super(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        this.accessOrder = accessOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   408
     * Returns {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param value value whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   412
     * @return {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public boolean containsValue(Object value) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   416
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   417
            V v = e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   418
            if (v == value || (value != null && value.equals(v)))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   419
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public V get(Object key) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   440
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   441
        if ((e = getNode(hash(key), key)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return null;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   443
        if (accessOrder)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   444
            afterNodeAccess(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return e.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
22058
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   449
     * {@inheritDoc}
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   450
     */
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   451
    public V getOrDefault(Object key, V defaultValue) {
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   452
       Node<K,V> e;
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   453
       if ((e = getNode(hash(key), key)) == null)
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   454
           return defaultValue;
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   455
       if (accessOrder)
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   456
           afterNodeAccess(e);
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   457
       return e.value;
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   458
   }
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   459
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   460
    /**
db159b144c98 8029795: LinkedHashMap.getOrDefault() doesn't update access order.
mduigou
parents: 19806
diff changeset
   461
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        super.clear();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   465
        head = tail = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   469
     * Returns {@code true} if this map should remove its eldest entry.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   470
     * This method is invoked by {@code put} and {@code putAll} after
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * inserting a new entry into the map.  It provides the implementor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * with the opportunity to remove the eldest entry each time a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * is added.  This is useful if the map represents a cache: it allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * the map to reduce memory consumption by deleting stale entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <p>Sample use: this override will allow the map to grow up to 100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * entries and then delete the eldest entry each time a new entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * added, maintaining a steady state of 100 entries.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   479
     * <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *     private static final int MAX_ENTRIES = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *     protected boolean removeEldestEntry(Map.Entry eldest) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   483
     *        return size() &gt; MAX_ENTRIES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *     }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   485
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>This method typically does not modify the map in any way,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * instead allowing the map to modify itself as directed by its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * return value.  It <i>is</i> permitted for this method to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * the map directly, but if it does so, it <i>must</i> return
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   491
     * {@code false} (indicating that the map should not attempt any
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   492
     * further modification).  The effects of returning {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * after modifying the map from within this method are unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   495
     * <p>This implementation merely returns {@code false} (so that this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * map acts like a normal map - the eldest element is never removed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param    eldest The least recently inserted entry in the map, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *           this is an access-ordered map, the least recently accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *           entry.  This is the entry that will be removed it this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   501
     *           method returns {@code true}.  If the map was empty prior
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   502
     *           to the {@code put} or {@code putAll} invocation resulting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *           in this invocation, this will be the entry that was just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *           inserted; in other words, if the map contains a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *           entry, the eldest entry is also the newest.
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   506
     * @return   {@code true} if the eldest entry should be removed
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   507
     *           from the map; {@code false} if it should be retained.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   512
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   513
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   514
     * Returns a {@link Set} view of the keys contained in this map.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   515
     * The set is backed by the map, so changes to the map are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   516
     * reflected in the set, and vice-versa.  If the map is modified
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   517
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   518
     * the iterator's own {@code remove} operation), the results of
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   519
     * the iteration are undefined.  The set supports element removal,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   520
     * which removes the corresponding mapping from the map, via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   521
     * {@code Iterator.remove}, {@code Set.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   522
     * {@code removeAll}, {@code retainAll}, and {@code clear}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   523
     * operations.  It does not support the {@code add} or {@code addAll}
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   524
     * operations.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   525
     * Its {@link Spliterator} typically provides faster sequential
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   526
     * performance but much poorer parallel performance than that of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   527
     * {@code HashMap}.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   528
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   529
     * @return a set view of the keys contained in this map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   530
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   531
    public Set<K> keySet() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   532
        Set<K> ks = keySet;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   533
        if (ks == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   534
            ks = new LinkedKeySet();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   535
            keySet = ks;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   536
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   537
        return ks;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   538
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   539
55391
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   540
    @Override
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   541
    final <T> T[] keysToArray(T[] a) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   542
        Object[] r = a;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   543
        int idx = 0;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   544
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   545
            r[idx++] = e.key;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   546
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   547
        return a;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   548
    }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   549
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   550
    @Override
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   551
    final <T> T[] valuesToArray(T[] a) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   552
        Object[] r = a;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   553
        int idx = 0;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   554
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   555
            r[idx++] = e.value;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   556
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   557
        return a;
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   558
    }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   559
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   560
    final class LinkedKeySet extends AbstractSet<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   561
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   562
        public final void clear()               { LinkedHashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   563
        public final Iterator<K> iterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   564
            return new LinkedKeyIterator();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   565
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   566
        public final boolean contains(Object o) { return containsKey(o); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   567
        public final boolean remove(Object key) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   568
            return removeNode(hash(key), key, null, false, true) != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   569
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   570
        public final Spliterator<K> spliterator()  {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   571
            return Spliterators.spliterator(this, Spliterator.SIZED |
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   572
                                            Spliterator.ORDERED |
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   573
                                            Spliterator.DISTINCT);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   574
        }
55391
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   575
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   576
        public Object[] toArray() {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   577
            return keysToArray(new Object[size]);
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   578
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   579
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   580
        public <T> T[] toArray(T[] a) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   581
            return keysToArray(prepareArray(a));
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   582
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   583
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   584
        public final void forEach(Consumer<? super K> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   585
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   586
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   587
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   588
            for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   589
                action.accept(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   590
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   591
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   592
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   593
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   594
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   595
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   596
     * Returns a {@link Collection} view of the values contained in this map.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   597
     * The collection is backed by the map, so changes to the map are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   598
     * reflected in the collection, and vice-versa.  If the map is
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   599
     * modified while an iteration over the collection is in progress
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   600
     * (except through the iterator's own {@code remove} operation),
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   601
     * the results of the iteration are undefined.  The collection
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   602
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   603
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   604
     * {@code Collection.remove}, {@code removeAll},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   605
     * {@code retainAll} and {@code clear} operations.  It does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   606
     * support the {@code add} or {@code addAll} operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   607
     * Its {@link Spliterator} typically provides faster sequential
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   608
     * performance but much poorer parallel performance than that of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   609
     * {@code HashMap}.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   610
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   611
     * @return a view of the values contained in this map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   612
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   613
    public Collection<V> values() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   614
        Collection<V> vs = values;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   615
        if (vs == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   616
            vs = new LinkedValues();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   617
            values = vs;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   618
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   619
        return vs;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   620
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   621
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   622
    final class LinkedValues extends AbstractCollection<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   623
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   624
        public final void clear()               { LinkedHashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   625
        public final Iterator<V> iterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   626
            return new LinkedValueIterator();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   627
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   628
        public final boolean contains(Object o) { return containsValue(o); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   629
        public final Spliterator<V> spliterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   630
            return Spliterators.spliterator(this, Spliterator.SIZED |
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   631
                                            Spliterator.ORDERED);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   632
        }
55391
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   633
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   634
        public Object[] toArray() {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   635
            return valuesToArray(new Object[size]);
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   636
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   637
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   638
        public <T> T[] toArray(T[] a) {
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   639
            return valuesToArray(prepareArray(a));
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   640
        }
1afe0cb93482 8225339: Optimize HashMap.keySet()/HashMap.values()/HashSet toArray() methods
tvaleev
parents: 49433
diff changeset
   641
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   642
        public final void forEach(Consumer<? super V> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   643
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   644
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   645
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   646
            for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   647
                action.accept(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   648
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   649
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   650
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   651
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   652
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   653
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   654
     * Returns a {@link Set} view of the mappings contained in this map.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   655
     * The set is backed by the map, so changes to the map are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   656
     * reflected in the set, and vice-versa.  If the map is modified
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   657
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   658
     * the iterator's own {@code remove} operation, or through the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   659
     * {@code setValue} operation on a map entry returned by the
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   660
     * iterator) the results of the iteration are undefined.  The set
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   661
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   662
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   663
     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   664
     * {@code clear} operations.  It does not support the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   665
     * {@code add} or {@code addAll} operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   666
     * Its {@link Spliterator} typically provides faster sequential
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   667
     * performance but much poorer parallel performance than that of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   668
     * {@code HashMap}.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   669
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   670
     * @return a set view of the mappings contained in this map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   671
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   672
    public Set<Map.Entry<K,V>> entrySet() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   673
        Set<Map.Entry<K,V>> es;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   674
        return (es = entrySet) == null ? (entrySet = new LinkedEntrySet()) : es;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   675
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   676
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   677
    final class LinkedEntrySet extends AbstractSet<Map.Entry<K,V>> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   678
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   679
        public final void clear()               { LinkedHashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   680
        public final Iterator<Map.Entry<K,V>> iterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   681
            return new LinkedEntryIterator();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   682
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   683
        public final boolean contains(Object o) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   684
            if (!(o instanceof Map.Entry))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   685
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   686
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   687
            Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   688
            Node<K,V> candidate = getNode(hash(key), key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   689
            return candidate != null && candidate.equals(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   690
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   691
        public final boolean remove(Object o) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   692
            if (o instanceof Map.Entry) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   693
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   694
                Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   695
                Object value = e.getValue();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   696
                return removeNode(hash(key), key, value, true, true) != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   697
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   698
            return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   699
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   700
        public final Spliterator<Map.Entry<K,V>> spliterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   701
            return Spliterators.spliterator(this, Spliterator.SIZED |
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   702
                                            Spliterator.ORDERED |
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   703
                                            Spliterator.DISTINCT);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   704
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   705
        public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   706
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   707
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   708
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   709
            for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   710
                action.accept(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   711
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   712
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   713
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   714
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   715
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   716
    // Map overrides
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   717
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   718
    public void forEach(BiConsumer<? super K, ? super V> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   719
        if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   720
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   721
        int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   722
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   723
            action.accept(e.key, e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   724
        if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   725
            throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   726
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   727
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   728
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   729
        if (function == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   730
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   731
        int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   732
        for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   733
            e.value = function.apply(e.key, e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   734
        if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   735
            throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   736
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   737
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   738
    // Iterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   739
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   740
    abstract class LinkedHashIterator {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   741
        LinkedHashMap.Entry<K,V> next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   742
        LinkedHashMap.Entry<K,V> current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   743
        int expectedModCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   744
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   745
        LinkedHashIterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   746
            next = head;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   747
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   748
            current = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   749
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   750
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   751
        public final boolean hasNext() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   752
            return next != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   753
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   754
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   755
        final LinkedHashMap.Entry<K,V> nextNode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   756
            LinkedHashMap.Entry<K,V> e = next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   757
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   758
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   759
            if (e == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   760
                throw new NoSuchElementException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   761
            current = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   762
            next = e.after;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   763
            return e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   764
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   765
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   766
        public final void remove() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   767
            Node<K,V> p = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   768
            if (p == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   769
                throw new IllegalStateException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   770
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   771
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   772
            current = null;
42361
b64a8f0c668e 8170733: HashMap.HashIterator.remove method does not use cached value for the hash code.
psandoz
parents: 34712
diff changeset
   773
            removeNode(p.hash, p.key, null, false, false);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   774
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   775
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   776
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   777
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   778
    final class LinkedKeyIterator extends LinkedHashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   779
        implements Iterator<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   780
        public final K next() { return nextNode().getKey(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   781
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   782
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   783
    final class LinkedValueIterator extends LinkedHashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   784
        implements Iterator<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   785
        public final V next() { return nextNode().value; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   786
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   787
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   788
    final class LinkedEntryIterator extends LinkedHashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   789
        implements Iterator<Map.Entry<K,V>> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   790
        public final Map.Entry<K,V> next() { return nextNode(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   791
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   792
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19435
diff changeset
   793
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
}