Java为什么无法对Map的地图(例如:Map< String,Map< String,String>>)进行序列化

编程入门 行业动态 更新时间:2024-10-11 21:31:21
本文介绍了Java为什么无法对Map的地图(例如:Map< String,Map< String,String>>)进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在JDK 1.7中使用HashMap,在使用 SonarQube 进行代码审查时,我遇到了一些问题.

We are using HashMap in JDK 1.7 and I face some issue during the code review with SonarQube.

请考虑以下示例:

public class SerializationTest implements Serializable { private Map<String,String> test1=new HashMap<>(); //Serializeable private Map<ANEnum,String> test2=new HashMap<>(); //Serializeable private Map<String,ASerializeableObject> test3=new HashMap<>(); //Serializeable private Map<String,Map<String,String>> test4=new HashMap<>(); //Not Serializeable private Map<ANEnum,Map<String,String>> test5=new HashMap<>(); //Not Serializeable private Map<String,Map<String, ASerializeableObject>> test6=new HashMap<>(); //Not Serializeable

声纳标记的最后三个HashMap标记为不是serializeable.声纳错误是(Make "test4" transient or serializable)

The Sonar mark last three HashMaps as not serializeable. The Sonar error is (Make "test4" transient or serializable)

据我猜想HashMap是serializeable,如果它的键和值是serializeable.但是似乎如果我将HashMap值设置为另一个HashMap,则原来的HashMap根本就不会是serializeable.

As far as I guessed the HashMap is serializeable if its key and value are serializeable. But it seems that if I set a HashMap value as another HashMap, the original HashMap will not be serializeable at all.

此声纳问题正确吗?!如果可以的话,我该如何解决?!

Is this Sonar Issue correct ?! If it is how can I fix it ?!

推荐答案

让我们逐行查看每一行:

Let's see each line, one by one:

private Map<String,String> test1=new HashMap<>();

键类型String是可序列化的.值类型String是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.

The key type, String, is serializable. The value type, String, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.

private Map<ANEnum,String> test2=new HashMap<>();

密钥类型ANEnum是可序列化的.值类型String是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.

The key type, ANEnum, is serializable. The value type, String, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.

private Map<String,ASerializeableObject> test3=new HashMap<>();

键类型String是可序列化的.值类型ASerializeableObject是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.

The key type, String, is serializable. The value type, ASerializeableObject, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.

private Map<String,Map<String,String>> test4=new HashMap<>();

键类型String是可序列化的.具体的Map类型HashMap可序列化. 但是值类型Map不能必需可序列化. Map的某些具体实现(例如HashMap)是可序列化的.其他一些则不是.因此,Sonar无法保证此字段可序列化,并发出警告.如果您确定只将可序列化的映射存储为值,则没问题.如果存储不可序列化的映射,则序列化将在运行时失败.

The key type, String, is serializable. The concrete Map type, HashMap, is serializable. But the value type, Map, is not necessarily serializable. Some concrete implementations of Map (like HashMap), are serializable. Some others are not. So Sonar can't guarantee that this field is serializable, and issues a warning. If you're sure that you will only store serializable maps as values, no problem. If you store non-serializable maps, then the serialization will fail at runtime.

private Map<ANEnum,Map<String,String>> test5=new HashMap<>(); //Not Serializeable

与以前相同的解释

private Map<String,Map<String, ASerializeableObject>> test6=new HashMap<>();

与以前相同的解释

请记住,Sonar只是一个工具,有时可以提供帮助,有时会造成阻碍.您应该控制住一切,并决定是否通过警告来改变自己.

Remember that Sonar is only a tool, which can sometimes help, and sometimes get in the way. You should be in control, and decide if a warning should make you change things, or not.

更多推荐

Java为什么无法对Map的地图(例如:Map&lt; String,Map&lt; String,String&gt;&gt;

本文发布于:2023-11-25 10:29:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地图   序列化   Map   Java   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!