Java-Collections


go to main page

(0)Are Objects returned by keySet(), entrySet() and values() not Serializable?
Are Objects returned by keySet(), entrySet() and values() not Serializable?
No.

Copy the values of the Set returned by keySet(), entrySet() or values() into a new HashSet or TreeSet object:
Set obj = new HashSet(myMap.keySet());

It is almost always incorrect for inner classes to implement Serializable. The semantics violate the principle of least astonishment: serializing the instance of the nested class implicitly serializes its enclosing instance, even though it may be inaccessible. More seriously, it is non-portable. The serialized form is dependent on the details of the compiler's internal representation of nested classes. See page 217 of "Effective Java Programming Language Guide" [Bloch, 2001, Addison-Wesley] for details.
TOP