< prev index next >

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

Print this page
imported patch loadFactor-isNaN


 289     }
 290 
 291     /**
 292      * Reconstitute the {@code HashSet} instance from a stream (that is,
 293      * deserialize it).
 294      */
 295     private void readObject(java.io.ObjectInputStream s)
 296         throws java.io.IOException, ClassNotFoundException {
 297         // Read in any hidden serialization magic
 298         s.defaultReadObject();
 299 
 300         // Read capacity and verify non-negative.
 301         int capacity = s.readInt();
 302         if (capacity < 0) {
 303             throw new InvalidObjectException("Illegal capacity: " +
 304                                              capacity);
 305         }
 306 
 307         // Read load factor and verify positive and non NaN.
 308         float loadFactor = s.readFloat();
 309         if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
 310             throw new InvalidObjectException("Illegal load factor: " +
 311                                              loadFactor);
 312         }
 313 
 314         // Read size and verify non-negative.
 315         int size = s.readInt();
 316         if (size < 0) {
 317             throw new InvalidObjectException("Illegal size: " +
 318                                              size);
 319         }
 320 
 321         // Set the capacity according to the size and load factor ensuring that
 322         // the HashMap is at least 25% full but clamping to maximum capacity.
 323         capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
 324                 HashMap.MAXIMUM_CAPACITY);
 325 
 326         // Constructing the backing map will lazily create an array when the first element is
 327         // added, so check it before construction. Call HashMap.tableSizeFor to compute the
 328         // actual allocation size. Check Map.Entry[].class since it's the nearest public type to
 329         // what is actually created.




 289     }
 290 
 291     /**
 292      * Reconstitute the {@code HashSet} instance from a stream (that is,
 293      * deserialize it).
 294      */
 295     private void readObject(java.io.ObjectInputStream s)
 296         throws java.io.IOException, ClassNotFoundException {
 297         // Read in any hidden serialization magic
 298         s.defaultReadObject();
 299 
 300         // Read capacity and verify non-negative.
 301         int capacity = s.readInt();
 302         if (capacity < 0) {
 303             throw new InvalidObjectException("Illegal capacity: " +
 304                                              capacity);
 305         }
 306 
 307         // Read load factor and verify positive and non NaN.
 308         float loadFactor = s.readFloat();
 309         if (!(loadFactor > 0)) {        // also checks for NaNs
 310             throw new InvalidObjectException("Illegal load factor: " +
 311                                              loadFactor);
 312         }
 313 
 314         // Read size and verify non-negative.
 315         int size = s.readInt();
 316         if (size < 0) {
 317             throw new InvalidObjectException("Illegal size: " +
 318                                              size);
 319         }
 320 
 321         // Set the capacity according to the size and load factor ensuring that
 322         // the HashMap is at least 25% full but clamping to maximum capacity.
 323         capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
 324                 HashMap.MAXIMUM_CAPACITY);
 325 
 326         // Constructing the backing map will lazily create an array when the first element is
 327         // added, so check it before construction. Call HashMap.tableSizeFor to compute the
 328         // actual allocation size. Check Map.Entry[].class since it's the nearest public type to
 329         // what is actually created.


< prev index next >