< prev index next >

src/java.base/share/classes/java/util/HashMap.java

Print this page
imported patch loadFactor-isNaN


 428      */
 429     final float loadFactor;
 430 
 431     /* ---------------- Public operations -------------- */
 432 
 433     /**
 434      * Constructs an empty {@code HashMap} with the specified initial
 435      * capacity and load factor.
 436      *
 437      * @param  initialCapacity the initial capacity
 438      * @param  loadFactor      the load factor
 439      * @throws IllegalArgumentException if the initial capacity is negative
 440      *         or the load factor is nonpositive
 441      */
 442     public HashMap(int initialCapacity, float loadFactor) {
 443         if (initialCapacity < 0)
 444             throw new IllegalArgumentException("Illegal initial capacity: " +
 445                                                initialCapacity);
 446         if (initialCapacity > MAXIMUM_CAPACITY)
 447             initialCapacity = MAXIMUM_CAPACITY;
 448         if (loadFactor <= 0 || Float.isNaN(loadFactor))
 449             throw new IllegalArgumentException("Illegal load factor: " +
 450                                                loadFactor);
 451         this.loadFactor = loadFactor;
 452         this.threshold = tableSizeFor(initialCapacity);
 453     }
 454 
 455     /**
 456      * Constructs an empty {@code HashMap} with the specified initial
 457      * capacity and the default load factor (0.75).
 458      *
 459      * @param  initialCapacity the initial capacity.
 460      * @throws IllegalArgumentException if the initial capacity is negative.
 461      */
 462     public HashMap(int initialCapacity) {
 463         this(initialCapacity, DEFAULT_LOAD_FACTOR);
 464     }
 465 
 466     /**
 467      * Constructs an empty {@code HashMap} with the default initial capacity
 468      * (16) and the default load factor (0.75).


1403         int buckets = capacity();
1404         // Write out the threshold, loadfactor, and any hidden stuff
1405         s.defaultWriteObject();
1406         s.writeInt(buckets);
1407         s.writeInt(size);
1408         internalWriteEntries(s);
1409     }
1410 
1411     /**
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 {
1420         // Read in the threshold (ignored), loadfactor, and any hidden stuff
1421         s.defaultReadObject();
1422         reinitialize();
1423         if (loadFactor <= 0 || Float.isNaN(loadFactor))
1424             throw new InvalidObjectException("Illegal load factor: " +
1425                                              loadFactor);
1426         s.readInt();                // Read and ignore number of buckets
1427         int mappings = s.readInt(); // Read number of mappings (size)
1428         if (mappings < 0)
1429             throw new InvalidObjectException("Illegal mappings count: " +
1430                                              mappings);
1431         else if (mappings > 0) { // (if zero, use defaults)
1432             // Size the table using given load factor only if within
1433             // range of 0.25...4.0
1434             float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
1435             float fc = (float)mappings / lf + 1.0f;
1436             int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
1437                        DEFAULT_INITIAL_CAPACITY :
1438                        (fc >= MAXIMUM_CAPACITY) ?
1439                        MAXIMUM_CAPACITY :
1440                        tableSizeFor((int)fc));
1441             float ft = (float)cap * lf;
1442             threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
1443                          (int)ft : Integer.MAX_VALUE);




 428      */
 429     final float loadFactor;
 430 
 431     /* ---------------- Public operations -------------- */
 432 
 433     /**
 434      * Constructs an empty {@code HashMap} with the specified initial
 435      * capacity and load factor.
 436      *
 437      * @param  initialCapacity the initial capacity
 438      * @param  loadFactor      the load factor
 439      * @throws IllegalArgumentException if the initial capacity is negative
 440      *         or the load factor is nonpositive
 441      */
 442     public HashMap(int initialCapacity, float loadFactor) {
 443         if (initialCapacity < 0)
 444             throw new IllegalArgumentException("Illegal initial capacity: " +
 445                                                initialCapacity);
 446         if (initialCapacity > MAXIMUM_CAPACITY)
 447             initialCapacity = MAXIMUM_CAPACITY;
 448         if (!(loadFactor > 0))          // also checks for NaNs
 449             throw new IllegalArgumentException("Illegal load factor: " +
 450                                                loadFactor);
 451         this.loadFactor = loadFactor;
 452         this.threshold = tableSizeFor(initialCapacity);
 453     }
 454 
 455     /**
 456      * Constructs an empty {@code HashMap} with the specified initial
 457      * capacity and the default load factor (0.75).
 458      *
 459      * @param  initialCapacity the initial capacity.
 460      * @throws IllegalArgumentException if the initial capacity is negative.
 461      */
 462     public HashMap(int initialCapacity) {
 463         this(initialCapacity, DEFAULT_LOAD_FACTOR);
 464     }
 465 
 466     /**
 467      * Constructs an empty {@code HashMap} with the default initial capacity
 468      * (16) and the default load factor (0.75).


1403         int buckets = capacity();
1404         // Write out the threshold, loadfactor, and any hidden stuff
1405         s.defaultWriteObject();
1406         s.writeInt(buckets);
1407         s.writeInt(size);
1408         internalWriteEntries(s);
1409     }
1410 
1411     /**
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 {
1420         // Read in the threshold (ignored), loadfactor, and any hidden stuff
1421         s.defaultReadObject();
1422         reinitialize();
1423         if (!(loadFactor > 0))          // also checks for NaNs
1424             throw new InvalidObjectException("Illegal load factor: " +
1425                                              loadFactor);
1426         s.readInt();                // Read and ignore number of buckets
1427         int mappings = s.readInt(); // Read number of mappings (size)
1428         if (mappings < 0)
1429             throw new InvalidObjectException("Illegal mappings count: " +
1430                                              mappings);
1431         else if (mappings > 0) { // (if zero, use defaults)
1432             // Size the table using given load factor only if within
1433             // range of 0.25...4.0
1434             float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
1435             float fc = (float)mappings / lf + 1.0f;
1436             int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
1437                        DEFAULT_INITIAL_CAPACITY :
1438                        (fc >= MAXIMUM_CAPACITY) ?
1439                        MAXIMUM_CAPACITY :
1440                        tableSizeFor((int)fc));
1441             float ft = (float)cap * lf;
1442             threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
1443                          (int)ft : Integer.MAX_VALUE);


< prev index next >