src/share/classes/java/util/WeakHashMap.java

Print this page




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.lang.ref.WeakReference;
  29 import java.lang.ref.ReferenceQueue;

  30 import java.util.function.Consumer;
  31 
  32 
  33 /**
  34  * Hash table based implementation of the <tt>Map</tt> interface, with
  35  * <em>weak keys</em>.
  36  * An entry in a <tt>WeakHashMap</tt> will automatically be removed when
  37  * its key is no longer in ordinary use.  More precisely, the presence of a
  38  * mapping for a given key will not prevent the key from being discarded by the
  39  * garbage collector, that is, made finalizable, finalized, and then reclaimed.
  40  * When a key has been discarded its entry is effectively removed from the map,
  41  * so this class behaves somewhat differently from other <tt>Map</tt>
  42  * implementations.
  43  *
  44  * <p> Both null values and the null key are supported. This class has
  45  * performance characteristics similar to those of the <tt>HashMap</tt>
  46  * class, and has the same efficiency parameters of <em>initial capacity</em>
  47  * and <em>load factor</em>.
  48  *
  49  * <p> Like most collection classes, this class is not synchronized.


 198                     ? Boolean.parseBoolean(hashSeedProp) : false;
 199             USE_HASHSEED = localBool;
 200         }
 201     }
 202 
 203     /**
 204      * A randomizing value associated with this instance that is applied to
 205      * hash code of keys to make hash collisions harder to find.
 206      *
 207      * Non-final so it can be set lazily, but be sure not to set more than once.
 208      */
 209     transient int hashSeed;
 210 
 211     /**
 212      * Initialize the hashing mask value.
 213      */
 214     final void initHashSeed() {
 215         if (sun.misc.VM.isBooted() && Holder.USE_HASHSEED) {
 216             // Do not set hashSeed more than once!
 217             // assert hashSeed == 0;
 218             hashSeed = sun.misc.Hashing.randomHashSeed(this);

 219         }
 220     }
 221 
 222     @SuppressWarnings("unchecked")
 223     private Entry<K,V>[] newTable(int n) {
 224         return (Entry<K,V>[]) new Entry<?,?>[n];
 225     }
 226 
 227     /**
 228      * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
 229      * capacity and the given load factor.
 230      *
 231      * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
 232      * @param  loadFactor      The load factor of the <tt>WeakHashMap</tt>
 233      * @throws IllegalArgumentException if the initial capacity is negative,
 234      *         or if the load factor is nonpositive.
 235      */
 236     public WeakHashMap(int initialCapacity, float loadFactor) {
 237         if (initialCapacity < 0)
 238             throw new IllegalArgumentException("Illegal Initial Capacity: "+




  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.lang.ref.WeakReference;
  29 import java.lang.ref.ReferenceQueue;
  30 import java.util.concurrent.ThreadLocalRandom;
  31 import java.util.function.Consumer;
  32 
  33 
  34 /**
  35  * Hash table based implementation of the <tt>Map</tt> interface, with
  36  * <em>weak keys</em>.
  37  * An entry in a <tt>WeakHashMap</tt> will automatically be removed when
  38  * its key is no longer in ordinary use.  More precisely, the presence of a
  39  * mapping for a given key will not prevent the key from being discarded by the
  40  * garbage collector, that is, made finalizable, finalized, and then reclaimed.
  41  * When a key has been discarded its entry is effectively removed from the map,
  42  * so this class behaves somewhat differently from other <tt>Map</tt>
  43  * implementations.
  44  *
  45  * <p> Both null values and the null key are supported. This class has
  46  * performance characteristics similar to those of the <tt>HashMap</tt>
  47  * class, and has the same efficiency parameters of <em>initial capacity</em>
  48  * and <em>load factor</em>.
  49  *
  50  * <p> Like most collection classes, this class is not synchronized.


 199                     ? Boolean.parseBoolean(hashSeedProp) : false;
 200             USE_HASHSEED = localBool;
 201         }
 202     }
 203 
 204     /**
 205      * A randomizing value associated with this instance that is applied to
 206      * hash code of keys to make hash collisions harder to find.
 207      *
 208      * Non-final so it can be set lazily, but be sure not to set more than once.
 209      */
 210     transient int hashSeed;
 211 
 212     /**
 213      * Initialize the hashing mask value.
 214      */
 215     final void initHashSeed() {
 216         if (sun.misc.VM.isBooted() && Holder.USE_HASHSEED) {
 217             // Do not set hashSeed more than once!
 218             // assert hashSeed == 0;
 219             int seed = ThreadLocalRandom.current().nextInt();
 220             hashSeed = (0 != seed) ? seed : 1;
 221         }
 222     }
 223 
 224     @SuppressWarnings("unchecked")
 225     private Entry<K,V>[] newTable(int n) {
 226         return (Entry<K,V>[]) new Entry<?,?>[n];
 227     }
 228 
 229     /**
 230      * Constructs a new, empty <tt>WeakHashMap</tt> with the given initial
 231      * capacity and the given load factor.
 232      *
 233      * @param  initialCapacity The initial capacity of the <tt>WeakHashMap</tt>
 234      * @param  loadFactor      The load factor of the <tt>WeakHashMap</tt>
 235      * @throws IllegalArgumentException if the initial capacity is negative,
 236      *         or if the load factor is nonpositive.
 237      */
 238     public WeakHashMap(int initialCapacity, float loadFactor) {
 239         if (initialCapacity < 0)
 240             throw new IllegalArgumentException("Illegal Initial Capacity: "+