3

Enum maps are represented internally as arrays. K[] keyUniverse array of keys and Object[] vals array of values. These arrays are transient. Can you tell me why?

4

1 回答 1

1

它们是瞬态的,以允许以不同(更好)的方式进行序列化。也是entrySet瞬态的。

private void writeObject(java.io.ObjectOutputStream s)
    throws java.io.IOException
{
    // Write out the key type and any hidden stuff
    s.defaultWriteObject();

    // Write out size (number of Mappings)
    s.writeInt(size);

    // Write out keys and values (alternating)
    for (Map.Entry<K,V> e :  entrySet()) {
        s.writeObject(e.getKey());
        s.writeObject(e.getValue());
    }
}

private void readObject(java.io.ObjectInputStream s)
    throws java.io.IOException, ClassNotFoundException
{
    // Read in the key type and any hidden stuff
    s.defaultReadObject();

    keyUniverse = getKeyUniverse(keyType);
    vals = new Object[keyUniverse.length];

    // Read in size (number of Mappings)
    int size = s.readInt();

    // Read the keys and values, and put the mappings in the HashMap
    for (int i = 0; i < size; i++) {
        K key = (K) s.readObject();
        V value = (V) s.readObject();
        put(key, value);
    }
}
于 2015-04-03T10:07:13.350 回答