Java Hashtable的count字段何时初始化?

时间:2018-11-29 08:57:15

标签: java hashtable

在阅读Java Hashtable源代码时,我注意到count的{​​{1}}字段在声明时未初始化,我发现在Hashtable方法中有此代码:

readObject

计数字段何时初始化?

1 个答案:

答案 0 :(得分:2)

readObject还为每个键值对调用reconstitutionPut(table, key, value),该方法将递增count

以下是带有相关行的相关代码:

private void readObject(java.io.ObjectInputStream s)
     throws IOException, ClassNotFoundException
{
    ...
    count = 0;

    // Read the number of elements and then all the key/value objects
    for (; elements > 0; elements--) {
        @SuppressWarnings("unchecked")
            K key = (K)s.readObject();
        @SuppressWarnings("unchecked")
            V value = (V)s.readObject();
        // synch could be eliminated for performance
        reconstitutionPut(table, key, value); // <---------------
    }
}

private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
    throws StreamCorruptedException
{
    if (value == null) {
        throw new java.io.StreamCorruptedException();
    }
    // Makes sure the key is not already in the hashtable.
    // This should not happen in deserialized version.
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
        if ((e.hash == hash) && e.key.equals(key)) {
            throw new java.io.StreamCorruptedException();
        }
    }
    // Creates the new entry.
    @SuppressWarnings("unchecked")
        Entry<K,V> e = (Entry<K,V>)tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;  // <---------------
}