简体   繁体   English

为什么并发HashMap可序列化

[英]Why Concurrent HashMap is Serializable

I was going through the source code of concurrent hashmap and found it is serializable. 我正在查看并发哈希图的源代码,发现它是可序列化的。 Also HashMap is serializable. HashMap也可以序列化。

Why concurrent hashmap/hashmap is serializable? 为什么并发哈希图/哈希图是可序列化的? I mean why this design choice was made. 我的意思是为什么要做出这种设计选择。

The HashMap is serializable, the class implements Serializable interface HashMap是可序列化的,该类实现Serializable接口

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: 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 您可以在以下链接中获得有关Java序列化的更多详细信息

http://www.tutorialspoint.com/java/java_serialization.htm http://www.tutorialspoint.com/java/java_serialization.htm

As other already said, HashMap is Serializable , but the Map or Collection interface doesn't force the implementation of every collection to be "serializable". 就像其他人已经说过的那样, HashMapSerializable ,但是Map或Collection接口并不强制每个collection的实现都是“可序列化的”。 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. 如果要创建自己的SetMap而不将其设置为可序列化,则由您选择,强迫人们这样做是没有意义的。

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. 但是我的猜测是,大多数实用程序类(例如Collection实现)都是可序列化的,因此每个人都不必创建自己的SerializableHashMap ,因为使用了这些类的人很多,而且有很多人对其进行序列化。 So it's kinda a basic feature for them to have. 因此,这对他们来说是一个基本功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM