privatevoidwriteObject(java.io.ObjectOutputStream s) throws java.io.IOException { // Write out any hidden serialization magic s.defaultWriteObject(); // Write out HashMap capacity and load factor s.writeInt(map.capacity()); s.writeFloat(map.loadFactor()); // Write out size s.writeInt(map.size()); // Write out all elements in the proper order. for (E e : map.keySet()) s.writeObject(e); }
privatevoidreadObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in any hidden serialization magic s.defaultReadObject(); // Read capacity and verify non-negative. intcapacity= s.readInt(); if (capacity < 0) { thrownewInvalidObjectException("Illegal capacity: " + capacity); } // Read load factor and verify positive and non NaN. floatloadFactor= s.readFloat(); if (loadFactor <= 0 || Float.isNaN(loadFactor)) { thrownewInvalidObjectException("Illegal load factor: " + loadFactor); } // Read size and verify non-negative. intsize= s.readInt(); if (size < 0) { thrownewInvalidObjectException("Illegal size: " + size); } // Set the capacity according to the size and load factor ensuring that // the HashMap is at least 25% full but clamping to maximum capacity. capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f), HashMap.MAXIMUM_CAPACITY); // Create backing HashMap map = (((HashSet<?>)this) instanceof LinkedHashSet ? newLinkedHashMap<E,Object>(capacity, loadFactor) : newHashMap<E,Object>(capacity, loadFactor)); // Read in all elements in the proper order. for (int i=0; i<size; i++) { @SuppressWarnings("unchecked") Ee= (E) s.readObject(); map.put(e, PRESENT); } }
1 2 3
public Spliterator<E> spliterator() { returnnewHashMap.KeySpliterator<E,Object>(map, 0, -1, 0, 0); }