简体   繁体   中英

java.util.Set, java.util.List Serializable issue

java.util.Set , java.util.List and other Collection interfaces are not serializable. Require a simple, direct solution to use this in a serializable 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> . The field type should be the least specific type that supports all the features you intend to use.

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.

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