1 /*
2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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.io.*;
29 import java.util.concurrent.ThreadLocalRandom;
30 import java.util.function.BiConsumer;
31 import java.util.function.Function;
32 import java.util.function.BiFunction;
33
34 /**
35 * This class implements a hash table, which maps keys to values. Any
36 * non-<code>null</code> object can be used as a key or as a value. <p>
37 *
38 * To successfully store and retrieve objects from a hashtable, the
39 * objects used as keys must implement the <code>hashCode</code>
40 * method and the <code>equals</code> method. <p>
41 *
42 * An instance of <code>Hashtable</code> has two parameters that affect its
43 * performance: <i>initial capacity</i> and <i>load factor</i>. The
44 * <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
45 * <i>initial capacity</i> is simply the capacity at the time the hash table
46 * is created. Note that the hash table is <i>open</i>: in the case of a "hash
47 * collision", a single bucket stores multiple entries, which must be searched
48 * sequentially. The <i>load factor</i> is a measure of how full the hash
49 * table is allowed to get before its capacity is automatically increased.
50 * The initial capacity and load factor parameters are merely hints to
51 * the implementation. The exact details as to when and whether the rehash
52 * method is invoked are implementation-dependent.<p>
1175 // Read the original length of the array and number of elements
1176 int origlength = s.readInt();
1177 int elements = s.readInt();
1178
1179 // Validate # of elements
1180 if (elements < 0)
1181 throw new StreamCorruptedException("Illegal # of Elements: " + elements);
1182
1183 // Clamp original length to be more than elements / loadFactor
1184 // (this is the invariant enforced with auto-growth)
1185 origlength = Math.max(origlength, (int)(elements / loadFactor) + 1);
1186
1187 // Compute new length with a bit of room 5% + 3 to grow but
1188 // no larger than the clamped original length. Make the length
1189 // odd if it's large enough, this helps distribute the entries.
1190 // Guard against the length ending up zero, that's not valid.
1191 int length = (int)((elements + elements / 20) / loadFactor) + 3;
1192 if (length > elements && (length & 1) == 0)
1193 length--;
1194 length = Math.min(length, origlength);
1195 table = new Entry<?,?>[length];
1196 threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
1197 count = 0;
1198
1199 // Read the number of elements and then all the key/value objects
1200 for (; elements > 0; elements--) {
1201 @SuppressWarnings("unchecked")
1202 K key = (K)s.readObject();
1203 @SuppressWarnings("unchecked")
1204 V value = (V)s.readObject();
1205 // sync is eliminated for performance
1206 reconstitutionPut(table, key, value);
1207 }
1208 }
1209
1210 /**
1211 * The put method used by readObject. This is provided because put
1212 * is overridable and should not be called in readObject since the
1213 * subclass will not yet be initialized.
1214 *
|
1 /*
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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.io.*;
29 import java.util.concurrent.ThreadLocalRandom;
30 import java.util.function.BiConsumer;
31 import java.util.function.Function;
32 import java.util.function.BiFunction;
33 import sun.misc.SharedSecrets;
34
35 /**
36 * This class implements a hash table, which maps keys to values. Any
37 * non-<code>null</code> object can be used as a key or as a value. <p>
38 *
39 * To successfully store and retrieve objects from a hashtable, the
40 * objects used as keys must implement the <code>hashCode</code>
41 * method and the <code>equals</code> method. <p>
42 *
43 * An instance of <code>Hashtable</code> has two parameters that affect its
44 * performance: <i>initial capacity</i> and <i>load factor</i>. The
45 * <i>capacity</i> is the number of <i>buckets</i> in the hash table, and the
46 * <i>initial capacity</i> is simply the capacity at the time the hash table
47 * is created. Note that the hash table is <i>open</i>: in the case of a "hash
48 * collision", a single bucket stores multiple entries, which must be searched
49 * sequentially. The <i>load factor</i> is a measure of how full the hash
50 * table is allowed to get before its capacity is automatically increased.
51 * The initial capacity and load factor parameters are merely hints to
52 * the implementation. The exact details as to when and whether the rehash
53 * method is invoked are implementation-dependent.<p>
1176 // Read the original length of the array and number of elements
1177 int origlength = s.readInt();
1178 int elements = s.readInt();
1179
1180 // Validate # of elements
1181 if (elements < 0)
1182 throw new StreamCorruptedException("Illegal # of Elements: " + elements);
1183
1184 // Clamp original length to be more than elements / loadFactor
1185 // (this is the invariant enforced with auto-growth)
1186 origlength = Math.max(origlength, (int)(elements / loadFactor) + 1);
1187
1188 // Compute new length with a bit of room 5% + 3 to grow but
1189 // no larger than the clamped original length. Make the length
1190 // odd if it's large enough, this helps distribute the entries.
1191 // Guard against the length ending up zero, that's not valid.
1192 int length = (int)((elements + elements / 20) / loadFactor) + 3;
1193 if (length > elements && (length & 1) == 0)
1194 length--;
1195 length = Math.min(length, origlength);
1196
1197 // Check Map.Entry[].class since it's the nearest public type to
1198 // what we're actually creating.
1199 SharedSecrets.getJavaOISAccess().checkArray(s, Map.Entry[].class, length);
1200 table = new Entry<?,?>[length];
1201 threshold = (int)Math.min(length * loadFactor, MAX_ARRAY_SIZE + 1);
1202 count = 0;
1203
1204 // Read the number of elements and then all the key/value objects
1205 for (; elements > 0; elements--) {
1206 @SuppressWarnings("unchecked")
1207 K key = (K)s.readObject();
1208 @SuppressWarnings("unchecked")
1209 V value = (V)s.readObject();
1210 // sync is eliminated for performance
1211 reconstitutionPut(table, key, value);
1212 }
1213 }
1214
1215 /**
1216 * The put method used by readObject. This is provided because put
1217 * is overridable and should not be called in readObject since the
1218 * subclass will not yet be initialized.
1219 *
|