Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialization ArrayList Java

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

like image 609
Nixit Patel Avatar asked Jan 09 '12 09:01

Nixit Patel


People also ask

Can ArrayList be serialized in Java?

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.

What is Java ArrayList serialization and deserialization?

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.

Are lists Serializable Java?

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.

Can an array be serialized?

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.


2 Answers

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.

like image 93
Ivaylo Petrov Avatar answered Oct 02 '22 03:10

Ivaylo Petrov


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.

like image 39
Spock Avatar answered Oct 02 '22 01:10

Spock