privatevoidreadObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { // Read in the threshold (ignored), loadfactor, and any hidden stuff s.defaultReadObject(); if (loadFactor <= 0 || Float.isNaN(loadFactor)) { thrownewInvalidObjectException("Illegal load factor: " + loadFactor); }
// set other fields that need values table = (Entry<K,V>[]) EMPTY_TABLE;
// Read in number of buckets s.readInt(); // ignored.
// Read number of mappings intmappings= s.readInt(); if (mappings < 0) thrownewInvalidObjectException("Illegal mappings count: " + mappings);
// capacity chosen by number of mappings and desired load (if >= 0.25) intcapacity= (int) Math.min( mappings * Math.min(1 / loadFactor, 4.0f), // we have limits... HashMap.MAXIMUM_CAPACITY);
// allocate the bucket array; if (mappings > 0) { inflateTable(capacity); } else { threshold = capacity; }
init(); // Give subclass a chance to do its thing.
// Read the keys and values, and put the mappings in the HashMap for (inti=0; i < mappings; i++) { Kkey= (K) s.readObject(); Vvalue= (V) s.readObject(); putForCreate(key, value); } }
/** * Look for preexisting entry for key. This will never happen for * clone or deserialize. It will only happen for construction if the * input Map is a sorted map whose ordering is inconsistent w/ equals. */ for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) { e.value = value; return; } }