How to Serialize ArrayList in Java?


Serialization is the process of converting an object into a stream of bytes so that it can be easily stored into a file or transmitted over a network. In Java, we can serialize an object by implementing the Serializable interface.

To serialize an ArrayList in Java, we need to follow these steps:

  • Create an ArrayList object and add some elements to it.
  • Create an ObjectOutputStream object to write the serialized data to a file or network stream.
  • Call the writeObject() method of the ObjectOutputStream object to serialize the ArrayList object.

Here's an example code snippet that demonstrates how to serialize an ArrayList in Java:

import java.io.*;
import java.util.ArrayList;

public class SerializeArrayList {
    public static void main(String[] args) {
        // Create an ArrayList object and add some elements to it
        ArrayList<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");

        try {
            // Create an ObjectOutputStream object to write the serialized data to a file or network stream
            FileOutputStream fos = new FileOutputStream("list.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            // Call the writeObject() method of the ObjectOutputStream object to serialize the ArrayList object
            oos.writeObject(list);

            // Close the ObjectOutputStream object
            oos.close();

            System.out.println("ArrayList has been serialized successfully");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

In this example, we have created an ArrayList object and added some elements to it. Then, we have created an ObjectOutputStream object to write the serialized data to a file named "list.ser". Finally, we have called the writeObject() method of the ObjectOutputStream object to serialize the ArrayList object.

Another way to serialize an ArrayList in Java is to use the Gson library. Gson is a Java library that can be used to convert Java Objects into their JSON representation. Here's an example code snippet that demonstrates how to serialize an ArrayList using Gson:

import com.google.gson.Gson;
import java.util.ArrayList;

public class SerializeArrayList {
    public static void main(String[] args) {
        // Create an ArrayList object and add some elements to it
        ArrayList<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");

        // Create a Gson object
        Gson gson = new Gson();

        // Convert the ArrayList object to JSON string
        String json = gson.toJson(list);

        System.out.println("Serialized ArrayList: " + json);
    }
}

In this example, we have created an ArrayList object and added some elements to it. Then, we have created a Gson object and used its toJson() method to convert the ArrayList object to a JSON string.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.