HashMap KeySet, EntrySet and values ​​are null and the table is not empty

I am reading a file data.ser

to fill in some HashMap

saved data.

Although Map

it matters that I should have, the values KeySet

, EntrySet

and the values ​​are zero. for example, HashMap

"Employees" looks like this when I test it in debug mode:

hashmap as shown with debugs inspect option:

Can someone help me figure out what's wrong with it? How is this situation possible?

An example of a null return value even though the map contains the value (Employee) with a key lookup (ID) im: problem example

+3


source to share


1 answer


This is UnmodifiableMap .

The items you mentioned are zero-initialized:

private transient Set<K> keySet = null;
private transient Set<Map.Entry<K,V>> entrySet = null;
private transient Collection<V> values = null;

      



Methods of the same name access the basemap to return nonzero values ​​and cache them in these variables the first time they are called.

public Set<K> keySet() {
    if (keySet==null)
    keySet = unmodifiableSet(m.keySet());
    return keySet;
}

public Set<Map.Entry<K,V>> entrySet() {
    if (entrySet==null)
    entrySet = new UnmodifiableEntrySet<K,V>(m.entrySet());
    return entrySet;
}

public Collection<V> values() {
    if (values==null)
    values = unmodifiableCollection(m.values());
    return values;
}

      

Therefore, if you call these three methods, you will see that these data members are no longer null.

+4


source







All Articles