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

685 views Asked by At

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>;
}
2

There are 2 answers

0
Anand S Kumar On

HashSet is serializable [Documentation] as long as all the objects it contains are serializable, so you need to make sure that Address class is serializable.

1
Patricia Shanahan On

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.