I was going through the source code of concurrent hashmap and found it is serializable. Also HashMap is serializable.
Why concurrent hashmap/hashmap is serializable? I mean why this design choice was made.
The HashMap is serializable, the class implements Serializable
interface
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
private static final long serialVersionUID = 362498820763181265L;
The same is ConcurrentHashMap:
public class ConcurrentHashMap<K,V> extends AbstractMap<K,V>
implements ConcurrentMap<K,V>, Serializable {
private static final long serialVersionUID = 7249069246763182397L;
you can get more detail about java serialize in the following link
As other already said, HashMap
is Serializable
, but the Map or Collection
interface doesn't force the implementation of every collection to be "serializable". If you want to create you're own Set
or Map
and not set it to serializable, it's your choice and it wouldn't make sense to force people to do it.
But my guess is, most utility classes like Collection
implementations are serializable so everyone doesn't have to create their own SerializableHashMap
, because these classes are used a lot and lots of people serialize them. So it's kinda a basic feature for them to have.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.