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

Comparing jsr166/src/main/java/util/HashMap.java (file contents):
Revision 1.1 by jsr166, Wed Aug 23 05:33:00 2017 UTC vs.
Revision 1.9 by jsr166, Sun Nov 11 16:27:28 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2 > * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4   *
5   * This code is free software; you can redistribute it and/or modify it
# Line 34 | Line 34 | import java.util.function.BiConsumer;
34   import java.util.function.BiFunction;
35   import java.util.function.Consumer;
36   import java.util.function.Function;
37 + // OPENJDK import jdk.internal.access.SharedSecrets;
38  
39   /**
40   * Hash table based implementation of the {@code Map} interface.  This
# Line 117 | Line 118 | import java.util.function.Function;
118   * should be used only to detect bugs.</i>
119   *
120   * <p>This class is a member of the
121 < * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
121 > * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
122   * Java Collections Framework</a>.
123   *
124   * @param <K> the type of keys maintained by this map
# Line 375 | Line 376 | public class HashMap<K,V> extends Abstra
376       * Returns a power of two size for the given target capacity.
377       */
378      static final int tableSizeFor(int cap) {
379 <        int n = cap - 1;
379 <        n |= n >>> 1;
380 <        n |= n >>> 2;
381 <        n |= n >>> 4;
382 <        n |= n >>> 8;
383 <        n |= n >>> 16;
379 >        int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1);
380          return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
381      }
382  
# Line 1267 | Line 1263 | public class HashMap<K,V> extends Abstra
1263      @Override
1264      public V merge(K key, V value,
1265                     BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
1266 <        if (value == null)
1271 <            throw new NullPointerException();
1272 <        if (remappingFunction == null)
1266 >        if (value == null || remappingFunction == null)
1267              throw new NullPointerException();
1268          int hash = hash(key);
1269          Node<K,V>[] tab; Node<K,V> first; int n, i;
# Line 1312 | Line 1306 | public class HashMap<K,V> extends Abstra
1306              else
1307                  removeNode(hash, key, null, false, true);
1308              return v;
1309 <        }
1316 <        if (value != null) {
1309 >        } else {
1310              if (t != null)
1311                  t.putTreeVal(this, tab, hash, key, value);
1312              else {
# Line 1324 | Line 1317 | public class HashMap<K,V> extends Abstra
1317              ++modCount;
1318              ++size;
1319              afterNodeInsertion(true);
1320 +            return value;
1321          }
1328        return value;
1322      }
1323  
1324      @Override
# Line 1394 | Line 1387 | public class HashMap<K,V> extends Abstra
1387      }
1388  
1389      /**
1390 <     * Save the state of the {@code HashMap} instance to a stream (i.e.,
1398 <     * serialize it).
1390 >     * Saves this map to a stream (that is, serializes it).
1391       *
1392 +     * @param s the stream
1393 +     * @throws IOException if an I/O error occurs
1394       * @serialData The <i>capacity</i> of the HashMap (the length of the
1395       *             bucket array) is emitted (int), followed by the
1396       *             <i>size</i> (an int, the number of key-value
# Line 1415 | Line 1409 | public class HashMap<K,V> extends Abstra
1409      }
1410  
1411      /**
1412 <     * Reconstitute the {@code HashMap} instance from a stream (i.e.,
1413 <     * deserialize it).
1412 >     * Reconstitutes this map from a stream (that is, deserializes it).
1413 >     * @param s the stream
1414 >     * @throws ClassNotFoundException if the class of a serialized object
1415 >     *         could not be found
1416 >     * @throws IOException if an I/O error occurs
1417       */
1418      private void readObject(java.io.ObjectInputStream s)
1419          throws IOException, ClassNotFoundException {
# Line 1444 | Line 1441 | public class HashMap<K,V> extends Abstra
1441              float ft = (float)cap * lf;
1442              threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
1443                           (int)ft : Integer.MAX_VALUE);
1444 +
1445 +            // Check Map.Entry[].class since it's the nearest public type to
1446 +            // what we're actually creating.
1447 +            jsr166.Platform.checkArray(s, Map.Entry[].class, cap);
1448              @SuppressWarnings({"rawtypes","unchecked"})
1449              Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
1450              table = tab;
# Line 2144 | Line 2145 | public class HashMap<K,V> extends Abstra
2145              if (replacement != p) {
2146                  TreeNode<K,V> pp = replacement.parent = p.parent;
2147                  if (pp == null)
2148 <                    root = replacement;
2148 >                    (root = replacement).red = false;
2149                  else if (p == pp.left)
2150                      pp.left = replacement;
2151                  else

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines