I want to serialize the Arraylist of type ArrayList<Class>
and the class contains two Arraylist of primitive type
public class Keyword {
private long id;
private long wid;
private String name;
private ArrayList<Integer> rank;
private int month;
private float avg;
private ArrayList<String> date;
... and so on
}
How can i do that?
Lots of thanks in advance
In Java, the ArrayList class implements a Serializable interface by default i.e., ArrayList is by default serialized. We can just use the ObjectOutputStream directly to serialize it.
In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly in order to serialize ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.
As pointed out already, most standard implementations of List are serializable. However you have to ensure that the objects referenced/contained within the list are also serializable. As pointed out already, List is an interface that does not extend Serializable or any interface that does so.
Arrays are relatively simple to serialize. The serialization code depends on whether the array is fixed length (and fixed-allocation) or variable-length (and dynamically allocated). The example contains both fixed-length and variable-length arrays.
If you want to use Serializable, you should have no problems, because ArrayList is Serializable just as String and every primitive type. public class Keyword implements Serializable {}
should do. You can read more about this method here. However there are a lot of other options for serialization. So please be more specific.
Implement and Interface
public class Keyword implements Serializable {}
and Simply Use the method of "Object output stream"
and "Object input stream" to Serialize and De-serialize the object.
and Arrylist can be Serialize as normal any data type can serialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With