< prev index next >

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

Print this page
imported patch loadFactor-isNaN


 167      * the Hashtable or otherwise modify its internal structure (e.g.,
 168      * rehash).  This field is used to make iterators on Collection-views of
 169      * the Hashtable fail-fast.  (See ConcurrentModificationException).
 170      */
 171     private transient int modCount = 0;
 172 
 173     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 174     private static final long serialVersionUID = 1421746759512286392L;
 175 
 176     /**
 177      * Constructs a new, empty hashtable with the specified initial
 178      * capacity and the specified load factor.
 179      *
 180      * @param      initialCapacity   the initial capacity of the hashtable.
 181      * @param      loadFactor        the load factor of the hashtable.
 182      * @exception  IllegalArgumentException  if the initial capacity is less
 183      *             than zero, or if the load factor is nonpositive.
 184      */
 185     public Hashtable(int initialCapacity, float loadFactor) {
 186         if (initialCapacity < 0)
 187             throw new IllegalArgumentException("Illegal Capacity: "+
 188                                                initialCapacity);
 189         if (loadFactor <= 0 || Float.isNaN(loadFactor))
 190             throw new IllegalArgumentException("Illegal Load: "+loadFactor);

 191 
 192         if (initialCapacity==0)
 193             initialCapacity = 1;
 194         this.loadFactor = loadFactor;
 195         table = new Entry<?,?>[initialCapacity];
 196         threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
 197     }
 198 
 199     /**
 200      * Constructs a new, empty hashtable with the specified initial capacity
 201      * and default load factor (0.75).
 202      *
 203      * @param     initialCapacity   the initial capacity of the hashtable.
 204      * @exception IllegalArgumentException if the initial capacity is less
 205      *              than zero.
 206      */
 207     public Hashtable(int initialCapacity) {
 208         this(initialCapacity, 0.75f);
 209     }
 210 


1252     }
1253 
1254     /**
1255      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
1256      */
1257     private void readObject(java.io.ObjectInputStream s)
1258             throws IOException, ClassNotFoundException {
1259         readHashtable(s);
1260     }
1261 
1262     /**
1263      * Perform deserialization of the Hashtable from an ObjectInputStream.
1264      * The Properties class overrides this method.
1265      */
1266     void readHashtable(java.io.ObjectInputStream s)
1267             throws IOException, ClassNotFoundException {
1268         // Read in the threshold and loadFactor
1269         s.defaultReadObject();
1270 
1271         // Validate loadFactor (ignore threshold - it will be re-computed)
1272         if (loadFactor <= 0 || Float.isNaN(loadFactor))
1273             throw new StreamCorruptedException("Illegal Load: " + loadFactor);

1274 
1275         // Read the original length of the array and number of elements
1276         int origlength = s.readInt();
1277         int elements = s.readInt();
1278 
1279         // Validate # of elements
1280         if (elements < 0)
1281             throw new StreamCorruptedException("Illegal # of Elements: " + elements);
1282 
1283         // Clamp original length to be more than elements / loadFactor
1284         // (this is the invariant enforced with auto-growth)
1285         origlength = Math.max(origlength, (int)(elements / loadFactor) + 1);
1286 
1287         // Compute new length with a bit of room 5% + 3 to grow but
1288         // no larger than the clamped original length.  Make the length
1289         // odd if it's large enough, this helps distribute the entries.
1290         // Guard against the length ending up zero, that's not valid.
1291         int length = (int)((elements + elements / 20) / loadFactor) + 3;
1292         if (length > elements && (length & 1) == 0)
1293             length--;




 167      * the Hashtable or otherwise modify its internal structure (e.g.,
 168      * rehash).  This field is used to make iterators on Collection-views of
 169      * the Hashtable fail-fast.  (See ConcurrentModificationException).
 170      */
 171     private transient int modCount = 0;
 172 
 173     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 174     private static final long serialVersionUID = 1421746759512286392L;
 175 
 176     /**
 177      * Constructs a new, empty hashtable with the specified initial
 178      * capacity and the specified load factor.
 179      *
 180      * @param      initialCapacity   the initial capacity of the hashtable.
 181      * @param      loadFactor        the load factor of the hashtable.
 182      * @exception  IllegalArgumentException  if the initial capacity is less
 183      *             than zero, or if the load factor is nonpositive.
 184      */
 185     public Hashtable(int initialCapacity, float loadFactor) {
 186         if (initialCapacity < 0)
 187             throw new IllegalArgumentException("Illegal Capacity: " +
 188                                                initialCapacity);
 189         if (!(loadFactor > 0))          // also checks for NaNs
 190             throw new IllegalArgumentException("Illegal load factor: " +
 191                                                loadFactor);
 192 
 193         if (initialCapacity==0)
 194             initialCapacity = 1;
 195         this.loadFactor = loadFactor;
 196         table = new Entry<?,?>[initialCapacity];
 197         threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
 198     }
 199 
 200     /**
 201      * Constructs a new, empty hashtable with the specified initial capacity
 202      * and default load factor (0.75).
 203      *
 204      * @param     initialCapacity   the initial capacity of the hashtable.
 205      * @exception IllegalArgumentException if the initial capacity is less
 206      *              than zero.
 207      */
 208     public Hashtable(int initialCapacity) {
 209         this(initialCapacity, 0.75f);
 210     }
 211 


1253     }
1254 
1255     /**
1256      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
1257      */
1258     private void readObject(java.io.ObjectInputStream s)
1259             throws IOException, ClassNotFoundException {
1260         readHashtable(s);
1261     }
1262 
1263     /**
1264      * Perform deserialization of the Hashtable from an ObjectInputStream.
1265      * The Properties class overrides this method.
1266      */
1267     void readHashtable(java.io.ObjectInputStream s)
1268             throws IOException, ClassNotFoundException {
1269         // Read in the threshold and loadFactor
1270         s.defaultReadObject();
1271 
1272         // Validate loadFactor (ignore threshold - it will be re-computed)
1273         if (!(loadFactor > 0))          // also checks for NaNs
1274             throw new StreamCorruptedException("Illegal load factor: " +
1275                                                loadFactor);
1276 
1277         // Read the original length of the array and number of elements
1278         int origlength = s.readInt();
1279         int elements = s.readInt();
1280 
1281         // Validate # of elements
1282         if (elements < 0)
1283             throw new StreamCorruptedException("Illegal # of Elements: " + elements);
1284 
1285         // Clamp original length to be more than elements / loadFactor
1286         // (this is the invariant enforced with auto-growth)
1287         origlength = Math.max(origlength, (int)(elements / loadFactor) + 1);
1288 
1289         // Compute new length with a bit of room 5% + 3 to grow but
1290         // no larger than the clamped original length.  Make the length
1291         // odd if it's large enough, this helps distribute the entries.
1292         // Guard against the length ending up zero, that's not valid.
1293         int length = (int)((elements + elements / 20) / loadFactor) + 3;
1294         if (length > elements && (length & 1) == 0)
1295             length--;


< prev index next >