Lines 23 and 28? How does it make that difference? How do we know that the first time we readInt it brings back the capacity and the next time we readInt it returns the size?
What kind of magic is this?
Also, what about readFloat? In the description it just says it reads a 32 bit float. How do we know that is the loadFactor? What happens if we accidentally readFloat multiple times or if it has been read before by somebody else and we don't know it?
It all seems so... random ?
private void writeObject(ObjectOutputStream s) throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out HashMap capacity and load factor
s.writeInt(HashMapReflectionHelper.<Integer>callHiddenMethod(map, "capacity"));
s.writeFloat(HashMapReflectionHelper.<Float>callHiddenMethod(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);
}
private void readObject(ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in HashMap capacity and load factor and create backing HashMap
int capacity = s.readInt();
float loadFactor = s.readFloat();
map = new HashMap<>(capacity, loadFactor);
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++) {
E e = (E) s.readObject();
map.put(e, PRESENT);
}
}