Reputation: 10825
If I have some Collection Collection<SomeClass> classes;
can it be serialized as is? Or would I have to declare it as an ArrayList, HashSet, or some other concrete implementation that implements Serializable? If so what is the general practice for doing this?
Upvotes: 4
Views: 1615
Reputation: 32680
As long as SomeClass
is serializable, any standard java.util.collection
of SomeClass
objects will be serializable.
I always assumed this was because the Collection
interface itself extended Serializable
, but - as you point out in your comment - this is not the case. So, I ran the following test:
public class Test {
static final String filename = "d:/desktop/employee.txt";
Collection makeColl(){
return new ArrayList();
}
void writeColl(Collection c){
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d:/desktop/employee.txt"));
out.writeObject(c);
out.close();
} catch(IOException i){i.printStackTrace();}
}
Collection readColl(){
Object that = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
that = in.readObject();
} catch(IOException | ClassNotFoundException i) { i.printStackTrace(); }
return that;
}
public static void main(String[] args) {
Test t = new Test();
t.writeColl(t.makeColl());
System.out.println(t.readColl().getClass());
}
and what gets printed is: "class java.util.ArrayList"
So, what we can take from this is that, while the Collection
interface does not extend Serializable
directly (and indeed neither do List
, Set
or other sub-interfaces), all standard implementations of these interfaces do. Further, when you make a call to serialize an object that implements Collection
, it will be serialized as an object of its concrete class (in the case of the above test, ArrayList
). This, of course, is always the case when you serialize an object known only by an interface to the serializing method.
So, to answer your question (which is an interesting one): Technically, no, a Collection
is not inherently serializable, and yes, one does need to be of a class that implements Serializable
(either a standard implementation or a custom one) in order to be serialized. However, in practice, no, you do not need to cast a collection to an implementation in order to serialize it, because Java does this for you.
To further clarify this: You can have a method that serializes arbitrary Collection
implementations (such as the one in my example), and it will only cause a NotSerializableException
in the case that a non-standard Collection
implementation that does not implement Serializable is passed into it.
Upvotes: 1
Reputation: 425278
All Collection
implementations from the JDK are Serializable
: Even though the Collection
interface doesn't extend Serializable
, all implementations (eg ArrayList
, HashSet
, etc) so implement Serializable
.
Unless you are using special (your own or from a library) non-Serializable Collection classes, and as long the class of the objects in the Collection (SomeClass
in your example) are themselves Serializable
, you don't need to worry.
Upvotes: 2