简体   繁体   English

java.util.Set,java.util.List可序列化的问题

[英]java.util.Set, java.util.List Serializable issue

java.util.Set , java.util.List and other Collection interfaces are not serializable. java.util.Set,java.util.List和其他Collection接口不可序列化。 Require a simple, direct solution to use this in a serializable POJO. 需要一个简单,直接的解决方案才能在可序列化的POJO中使用它。

public class Employee implements Serializable{
    private int id;
    private String name;
    private Set<Address> address= new HashSet<Address>;
}

HashSet是可序列化的[ 文档 ],只要它包含的所有对象都可以序列化,那么您需要确保Address类是可序列化的。

In this particular case there are two solutions. 在这种特殊情况下,有两种解决方案。 One is the approach used in the possible duplicate's answers. 一种是在可能重复的答案中使用的方法。 The other is to make the field type HashSet<Address> rather than Set<Address> . 另一种是使字段类型为HashSet<Address>而不是Set<Address> The field type should be the least specific type that supports all the features you intend to use. 字段类型应该是支持您打算使用的所有功能的最不具体的类型。

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

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