src/java.base/share/classes/java/util/HashMap.java
changeset 47304 3f5f9bc0bdc2
parent 47216 71c04702a3d5
child 47423 4fc2a4a29f3d
equal deleted inserted replaced
47303:e0637258a133 47304:3f5f9bc0bdc2
   488         this.loadFactor = DEFAULT_LOAD_FACTOR;
   488         this.loadFactor = DEFAULT_LOAD_FACTOR;
   489         putMapEntries(m, false);
   489         putMapEntries(m, false);
   490     }
   490     }
   491 
   491 
   492     /**
   492     /**
   493      * Implements Map.putAll and Map constructor
   493      * Implements Map.putAll and Map constructor.
   494      *
   494      *
   495      * @param m the map
   495      * @param m the map
   496      * @param evict false when initially constructing this map, else
   496      * @param evict false when initially constructing this map, else
   497      * true (relayed to method afterNodeInsertion).
   497      * true (relayed to method afterNodeInsertion).
   498      */
   498      */
   555         Node<K,V> e;
   555         Node<K,V> e;
   556         return (e = getNode(hash(key), key)) == null ? null : e.value;
   556         return (e = getNode(hash(key), key)) == null ? null : e.value;
   557     }
   557     }
   558 
   558 
   559     /**
   559     /**
   560      * Implements Map.get and related methods
   560      * Implements Map.get and related methods.
   561      *
   561      *
   562      * @param hash hash for key
   562      * @param hash hash for key
   563      * @param key the key
   563      * @param key the key
   564      * @return the node, or null if none
   564      * @return the node, or null if none
   565      */
   565      */
   610     public V put(K key, V value) {
   610     public V put(K key, V value) {
   611         return putVal(hash(key), key, value, false, true);
   611         return putVal(hash(key), key, value, false, true);
   612     }
   612     }
   613 
   613 
   614     /**
   614     /**
   615      * Implements Map.put and related methods
   615      * Implements Map.put and related methods.
   616      *
   616      *
   617      * @param hash hash for key
   617      * @param hash hash for key
   618      * @param key the key
   618      * @param key the key
   619      * @param value the value to put
   619      * @param value the value to put
   620      * @param onlyIfAbsent if true, don't change existing value
   620      * @param onlyIfAbsent if true, don't change existing value
   698             newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
   698             newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
   699                       (int)ft : Integer.MAX_VALUE);
   699                       (int)ft : Integer.MAX_VALUE);
   700         }
   700         }
   701         threshold = newThr;
   701         threshold = newThr;
   702         @SuppressWarnings({"rawtypes","unchecked"})
   702         @SuppressWarnings({"rawtypes","unchecked"})
   703             Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
   703         Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
   704         table = newTab;
   704         table = newTab;
   705         if (oldTab != null) {
   705         if (oldTab != null) {
   706             for (int j = 0; j < oldCap; ++j) {
   706             for (int j = 0; j < oldCap; ++j) {
   707                 Node<K,V> e;
   707                 Node<K,V> e;
   708                 if ((e = oldTab[j]) != null) {
   708                 if ((e = oldTab[j]) != null) {
   798         return (e = removeNode(hash(key), key, null, false, true)) == null ?
   798         return (e = removeNode(hash(key), key, null, false, true)) == null ?
   799             null : e.value;
   799             null : e.value;
   800     }
   800     }
   801 
   801 
   802     /**
   802     /**
   803      * Implements Map.remove and related methods
   803      * Implements Map.remove and related methods.
   804      *
   804      *
   805      * @param hash hash for key
   805      * @param hash hash for key
   806      * @param key the key
   806      * @param key the key
   807      * @param value the value to match if matchValue, else ignored
   807      * @param value the value to match if matchValue, else ignored
   808      * @param matchValue if true only remove if value is equal
   808      * @param matchValue if true only remove if value is equal
   873      *         specified value
   873      *         specified value
   874      */
   874      */
   875     public boolean containsValue(Object value) {
   875     public boolean containsValue(Object value) {
   876         Node<K,V>[] tab; V v;
   876         Node<K,V>[] tab; V v;
   877         if ((tab = table) != null && size > 0) {
   877         if ((tab = table) != null && size > 0) {
   878             for (Node<K, V> e : tab) {
   878             for (Node<K,V> e : tab) {
   879                 for (; e != null; e = e.next) {
   879                 for (; e != null; e = e.next) {
   880                     if ((v = e.value) == value ||
   880                     if ((v = e.value) == value ||
   881                         (value != null && value.equals(v)))
   881                         (value != null && value.equals(v)))
   882                         return true;
   882                         return true;
   883                 }
   883                 }
   925             Node<K,V>[] tab;
   925             Node<K,V>[] tab;
   926             if (action == null)
   926             if (action == null)
   927                 throw new NullPointerException();
   927                 throw new NullPointerException();
   928             if (size > 0 && (tab = table) != null) {
   928             if (size > 0 && (tab = table) != null) {
   929                 int mc = modCount;
   929                 int mc = modCount;
   930                 for (Node<K, V> e : tab) {
   930                 for (Node<K,V> e : tab) {
   931                     for (; e != null; e = e.next)
   931                     for (; e != null; e = e.next)
   932                         action.accept(e.key);
   932                         action.accept(e.key);
   933                 }
   933                 }
   934                 if (modCount != mc)
   934                 if (modCount != mc)
   935                     throw new ConcurrentModificationException();
   935                     throw new ConcurrentModificationException();
   973             Node<K,V>[] tab;
   973             Node<K,V>[] tab;
   974             if (action == null)
   974             if (action == null)
   975                 throw new NullPointerException();
   975                 throw new NullPointerException();
   976             if (size > 0 && (tab = table) != null) {
   976             if (size > 0 && (tab = table) != null) {
   977                 int mc = modCount;
   977                 int mc = modCount;
   978                 for (Node<K, V> e : tab) {
   978                 for (Node<K,V> e : tab) {
   979                     for (; e != null; e = e.next)
   979                     for (; e != null; e = e.next)
   980                         action.accept(e.value);
   980                         action.accept(e.value);
   981                 }
   981                 }
   982                 if (modCount != mc)
   982                 if (modCount != mc)
   983                     throw new ConcurrentModificationException();
   983                     throw new ConcurrentModificationException();
  1036             Node<K,V>[] tab;
  1036             Node<K,V>[] tab;
  1037             if (action == null)
  1037             if (action == null)
  1038                 throw new NullPointerException();
  1038                 throw new NullPointerException();
  1039             if (size > 0 && (tab = table) != null) {
  1039             if (size > 0 && (tab = table) != null) {
  1040                 int mc = modCount;
  1040                 int mc = modCount;
  1041                 for (Node<K, V> e : tab) {
  1041                 for (Node<K,V> e : tab) {
  1042                     for (; e != null; e = e.next)
  1042                     for (; e != null; e = e.next)
  1043                         action.accept(e);
  1043                         action.accept(e);
  1044                 }
  1044                 }
  1045                 if (modCount != mc)
  1045                 if (modCount != mc)
  1046                     throw new ConcurrentModificationException();
  1046                     throw new ConcurrentModificationException();
  1333         Node<K,V>[] tab;
  1333         Node<K,V>[] tab;
  1334         if (action == null)
  1334         if (action == null)
  1335             throw new NullPointerException();
  1335             throw new NullPointerException();
  1336         if (size > 0 && (tab = table) != null) {
  1336         if (size > 0 && (tab = table) != null) {
  1337             int mc = modCount;
  1337             int mc = modCount;
  1338             for (Node<K, V> e : tab) {
  1338             for (Node<K,V> e : tab) {
  1339                 for (; e != null; e = e.next)
  1339                 for (; e != null; e = e.next)
  1340                     action.accept(e.key, e.value);
  1340                     action.accept(e.key, e.value);
  1341             }
  1341             }
  1342             if (modCount != mc)
  1342             if (modCount != mc)
  1343                 throw new ConcurrentModificationException();
  1343                 throw new ConcurrentModificationException();
  1349         Node<K,V>[] tab;
  1349         Node<K,V>[] tab;
  1350         if (function == null)
  1350         if (function == null)
  1351             throw new NullPointerException();
  1351             throw new NullPointerException();
  1352         if (size > 0 && (tab = table) != null) {
  1352         if (size > 0 && (tab = table) != null) {
  1353             int mc = modCount;
  1353             int mc = modCount;
  1354             for (Node<K, V> e : tab) {
  1354             for (Node<K,V> e : tab) {
  1355                 for (; e != null; e = e.next) {
  1355                 for (; e != null; e = e.next) {
  1356                     e.value = function.apply(e.key, e.value);
  1356                     e.value = function.apply(e.key, e.value);
  1357                 }
  1357                 }
  1358             }
  1358             }
  1359             if (modCount != mc)
  1359             if (modCount != mc)
  1392             (threshold > 0) ? threshold :
  1392             (threshold > 0) ? threshold :
  1393             DEFAULT_INITIAL_CAPACITY;
  1393             DEFAULT_INITIAL_CAPACITY;
  1394     }
  1394     }
  1395 
  1395 
  1396     /**
  1396     /**
  1397      * Save the state of the {@code HashMap} instance to a stream (i.e.,
  1397      * Saves this map to a stream (that is, serializes it).
  1398      * serialize it).
  1398      *
  1399      *
  1399      * @param s the stream
       
  1400      * @throws IOException if an I/O error occurs
  1400      * @serialData The <i>capacity</i> of the HashMap (the length of the
  1401      * @serialData The <i>capacity</i> of the HashMap (the length of the
  1401      *             bucket array) is emitted (int), followed by the
  1402      *             bucket array) is emitted (int), followed by the
  1402      *             <i>size</i> (an int, the number of key-value
  1403      *             <i>size</i> (an int, the number of key-value
  1403      *             mappings), followed by the key (Object) and value (Object)
  1404      *             mappings), followed by the key (Object) and value (Object)
  1404      *             for each key-value mapping.  The key-value mappings are
  1405      *             for each key-value mapping.  The key-value mappings are
  1413         s.writeInt(size);
  1414         s.writeInt(size);
  1414         internalWriteEntries(s);
  1415         internalWriteEntries(s);
  1415     }
  1416     }
  1416 
  1417 
  1417     /**
  1418     /**
  1418      * Reconstitute the {@code HashMap} instance from a stream (i.e.,
  1419      * Reconstitutes this map from a stream (that is, deserializes it).
  1419      * deserialize it).
  1420      * @param s the stream
       
  1421      * @throws ClassNotFoundException if the class of a serialized object
       
  1422      *         could not be found
       
  1423      * @throws IOException if an I/O error occurs
  1420      */
  1424      */
  1421     private void readObject(java.io.ObjectInputStream s)
  1425     private void readObject(java.io.ObjectInputStream s)
  1422         throws IOException, ClassNotFoundException {
  1426         throws IOException, ClassNotFoundException {
  1423         // Read in the threshold (ignored), loadfactor, and any hidden stuff
  1427         // Read in the threshold (ignored), loadfactor, and any hidden stuff
  1424         s.defaultReadObject();
  1428         s.defaultReadObject();
  1443                        tableSizeFor((int)fc));
  1447                        tableSizeFor((int)fc));
  1444             float ft = (float)cap * lf;
  1448             float ft = (float)cap * lf;
  1445             threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
  1449             threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
  1446                          (int)ft : Integer.MAX_VALUE);
  1450                          (int)ft : Integer.MAX_VALUE);
  1447             @SuppressWarnings({"rawtypes","unchecked"})
  1451             @SuppressWarnings({"rawtypes","unchecked"})
  1448                 Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
  1452             Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
  1449             table = tab;
  1453             table = tab;
  1450 
  1454 
  1451             // Read the keys and values, and put the mappings in the HashMap
  1455             // Read the keys and values, and put the mappings in the HashMap
  1452             for (int i = 0; i < mappings; i++) {
  1456             for (int i = 0; i < mappings; i++) {
  1453                 @SuppressWarnings("unchecked")
  1457                 @SuppressWarnings("unchecked")
  1828 
  1832 
  1829     // Called only from writeObject, to ensure compatible ordering.
  1833     // Called only from writeObject, to ensure compatible ordering.
  1830     void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
  1834     void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
  1831         Node<K,V>[] tab;
  1835         Node<K,V>[] tab;
  1832         if (size > 0 && (tab = table) != null) {
  1836         if (size > 0 && (tab = table) != null) {
  1833             for (Node<K, V> e : tab) {
  1837             for (Node<K,V> e : tab) {
  1834                 for (; e != null; e = e.next) {
  1838                 for (; e != null; e = e.next) {
  1835                     s.writeObject(e.key);
  1839                     s.writeObject(e.key);
  1836                     s.writeObject(e.value);
  1840                     s.writeObject(e.value);
  1837                 }
  1841                 }
  1838             }
  1842             }
  1949             return d;
  1953             return d;
  1950         }
  1954         }
  1951 
  1955 
  1952         /**
  1956         /**
  1953          * Forms tree of the nodes linked from this node.
  1957          * Forms tree of the nodes linked from this node.
  1954          * @return root of tree
       
  1955          */
  1958          */
  1956         final void treeify(Node<K,V>[] tab) {
  1959         final void treeify(Node<K,V>[] tab) {
  1957             TreeNode<K,V> root = null;
  1960             TreeNode<K,V> root = null;
  1958             for (TreeNode<K,V> x = this, next; x != null; x = next) {
  1961             for (TreeNode<K,V> x = this, next; x != null; x = next) {
  1959                 next = (TreeNode<K,V>)x.next;
  1962                 next = (TreeNode<K,V>)x.next;
  2087                 succ.prev = pred;
  2090                 succ.prev = pred;
  2088             if (first == null)
  2091             if (first == null)
  2089                 return;
  2092                 return;
  2090             if (root.parent != null)
  2093             if (root.parent != null)
  2091                 root = root.root();
  2094                 root = root.root();
  2092             if (root == null || root.right == null ||
  2095             if (root == null
  2093                 (rl = root.left) == null || rl.left == null) {
  2096                 || (movable
       
  2097                     && (root.right == null
       
  2098                         || (rl = root.left) == null
       
  2099                         || rl.left == null))) {
  2094                 tab[index] = first.untreeify(map);  // too small
  2100                 tab[index] = first.untreeify(map);  // too small
  2095                 return;
  2101                 return;
  2096             }
  2102             }
  2097             TreeNode<K,V> p = this, pl = left, pr = right, replacement;
  2103             TreeNode<K,V> p = this, pl = left, pr = right, replacement;
  2098             if (pl != null && pr != null) {
  2104             if (pl != null && pr != null) {
  2317             }
  2323             }
  2318         }
  2324         }
  2319 
  2325 
  2320         static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
  2326         static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
  2321                                                    TreeNode<K,V> x) {
  2327                                                    TreeNode<K,V> x) {
  2322             for (TreeNode<K,V> xp, xpl, xpr;;)  {
  2328             for (TreeNode<K,V> xp, xpl, xpr;;) {
  2323                 if (x == null || x == root)
  2329                 if (x == null || x == root)
  2324                     return root;
  2330                     return root;
  2325                 else if ((xp = x.parent) == null) {
  2331                 else if ((xp = x.parent) == null) {
  2326                     x.red = false;
  2332                     x.red = false;
  2327                     return x;
  2333                     return x;