< prev index next >

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

Print this page




 154      */
 155     private int threshold;
 156 
 157     /**
 158      * The load factor for the hashtable.
 159      *
 160      * @serial
 161      */
 162     private float loadFactor;
 163 
 164     /**
 165      * The number of times this Hashtable has been structurally modified
 166      * Structural modifications are those that change the number of entries in
 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;


1184             }
1185         }
1186 
1187         if (value != null) {
1188             addEntry(hash, key, value, index);
1189         }
1190 
1191         return value;
1192     }
1193 
1194     /**
1195      * Save the state of the Hashtable to a stream (i.e., serialize it).
1196      *
1197      * @serialData The <i>capacity</i> of the Hashtable (the length of the
1198      *             bucket array) is emitted (int), followed by the
1199      *             <i>size</i> of the Hashtable (the number of key-value
1200      *             mappings), followed by the key (Object) and value (Object)
1201      *             for each key-value mapping represented by the Hashtable
1202      *             The key-value mappings are emitted in no particular order.
1203      */

1204     private void writeObject(java.io.ObjectOutputStream s)
1205             throws IOException {
1206         writeHashtable(s);
1207     }
1208 
1209     /**
1210      * Perform serialization of the Hashtable to an ObjectOutputStream.
1211      * The Properties class overrides this method.
1212      */
1213     void writeHashtable(java.io.ObjectOutputStream s)
1214             throws IOException {
1215         Entry<Object, Object> entryStack = null;
1216 
1217         synchronized (this) {
1218             // Write out the threshold and loadFactor
1219             s.defaultWriteObject();
1220 
1221             // Write out the length and count of elements
1222             s.writeInt(table.length);
1223             s.writeInt(count);


1237         while (entryStack != null) {
1238             s.writeObject(entryStack.key);
1239             s.writeObject(entryStack.value);
1240             entryStack = entryStack.next;
1241         }
1242     }
1243 
1244     /**
1245      * Called by Properties to write out a simulated threshold and loadfactor.
1246      */
1247     final void defaultWriteHashtable(java.io.ObjectOutputStream s, int length,
1248             float loadFactor) throws IOException {
1249         this.threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
1250         this.loadFactor = loadFactor;
1251         s.defaultWriteObject();
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();




 154      */
 155     private int threshold;
 156 
 157     /**
 158      * The load factor for the hashtable.
 159      *
 160      * @serial
 161      */
 162     private float loadFactor;
 163 
 164     /**
 165      * The number of times this Hashtable has been structurally modified
 166      * Structural modifications are those that change the number of entries in
 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     @java.io.Serial
 175     private static final long serialVersionUID = 1421746759512286392L;
 176 
 177     /**
 178      * Constructs a new, empty hashtable with the specified initial
 179      * capacity and the specified load factor.
 180      *
 181      * @param      initialCapacity   the initial capacity of the hashtable.
 182      * @param      loadFactor        the load factor of the hashtable.
 183      * @exception  IllegalArgumentException  if the initial capacity is less
 184      *             than zero, or if the load factor is nonpositive.
 185      */
 186     public Hashtable(int initialCapacity, float loadFactor) {
 187         if (initialCapacity < 0)
 188             throw new IllegalArgumentException("Illegal Capacity: "+
 189                                                initialCapacity);
 190         if (loadFactor <= 0 || Float.isNaN(loadFactor))
 191             throw new IllegalArgumentException("Illegal Load: "+loadFactor);
 192 
 193         if (initialCapacity==0)
 194             initialCapacity = 1;


1185             }
1186         }
1187 
1188         if (value != null) {
1189             addEntry(hash, key, value, index);
1190         }
1191 
1192         return value;
1193     }
1194 
1195     /**
1196      * Save the state of the Hashtable to a stream (i.e., serialize it).
1197      *
1198      * @serialData The <i>capacity</i> of the Hashtable (the length of the
1199      *             bucket array) is emitted (int), followed by the
1200      *             <i>size</i> of the Hashtable (the number of key-value
1201      *             mappings), followed by the key (Object) and value (Object)
1202      *             for each key-value mapping represented by the Hashtable
1203      *             The key-value mappings are emitted in no particular order.
1204      */
1205     @java.io.Serial
1206     private void writeObject(java.io.ObjectOutputStream s)
1207             throws IOException {
1208         writeHashtable(s);
1209     }
1210 
1211     /**
1212      * Perform serialization of the Hashtable to an ObjectOutputStream.
1213      * The Properties class overrides this method.
1214      */
1215     void writeHashtable(java.io.ObjectOutputStream s)
1216             throws IOException {
1217         Entry<Object, Object> entryStack = null;
1218 
1219         synchronized (this) {
1220             // Write out the threshold and loadFactor
1221             s.defaultWriteObject();
1222 
1223             // Write out the length and count of elements
1224             s.writeInt(table.length);
1225             s.writeInt(count);


1239         while (entryStack != null) {
1240             s.writeObject(entryStack.key);
1241             s.writeObject(entryStack.value);
1242             entryStack = entryStack.next;
1243         }
1244     }
1245 
1246     /**
1247      * Called by Properties to write out a simulated threshold and loadfactor.
1248      */
1249     final void defaultWriteHashtable(java.io.ObjectOutputStream s, int length,
1250             float loadFactor) throws IOException {
1251         this.threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
1252         this.loadFactor = loadFactor;
1253         s.defaultWriteObject();
1254     }
1255 
1256     /**
1257      * Reconstitute the Hashtable from a stream (i.e., deserialize it).
1258      */
1259     @java.io.Serial
1260     private void readObject(java.io.ObjectInputStream s)
1261             throws IOException, ClassNotFoundException {
1262         readHashtable(s);
1263     }
1264 
1265     /**
1266      * Perform deserialization of the Hashtable from an ObjectInputStream.
1267      * The Properties class overrides this method.
1268      */
1269     void readHashtable(java.io.ObjectInputStream s)
1270             throws IOException, ClassNotFoundException {
1271         // Read in the threshold and loadFactor
1272         s.defaultReadObject();
1273 
1274         // Validate loadFactor (ignore threshold - it will be re-computed)
1275         if (loadFactor <= 0 || Float.isNaN(loadFactor))
1276             throw new StreamCorruptedException("Illegal Load: " + loadFactor);
1277 
1278         // Read the original length of the array and number of elements
1279         int origlength = s.readInt();


< prev index next >