How to Serialize ArrayList in Java? - GeeksforGeeks

15 Dec, 2020
ArrayList is a class under the collections framework of java. It is present in java.util package. An ArrayList is a re-sizable array in java i.e., unlike an array, the size of an ArrayList can be modified dynamically according to our requirement. Also, the ArrayList class provides many useful methods to perform insertion, deletion, and many other operations on large amounts of data.
What is serialization?
To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implement either the java.io.Serializable interface or its sub interface, java.io.Externalizable. When an object is serialized, information that identifies its class is recorded in the serialized stream.
Serializing ArrayList:
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.
Java
// Java program to demonstrate serialization of ArrayList  import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.util.ArrayList;  public class ArrayListEx {      // method to serialize an ArrayList object    static void serializeArrayList()    {          // an ArrayList        // object "namesList"        // is created        ArrayList<String> namesList            = new ArrayList<String>();          // adding the data into the ArrayList        namesList.add("Geeks");        namesList.add("for");        namesList.add("Geeks");          try {            // an OutputStream file            // "namesListData" is            // created            FileOutputStream fos                = new FileOutputStream("namesListData");              // an ObjectOutputStream object is            // created on the FileOutputStream            // object            ObjectOutputStream oos                = new ObjectOutputStream(fos);              // calling the writeObject()            // method of the            // ObjectOutputStream on the            // OutputStream file "namesList"            oos.writeObject(namesList);              // close the ObjectOutputStream            oos.close();              // close the OutputStream file            fos.close();              System.out.println("namesList serialized");        }        catch (IOException ioe) {            ioe.printStackTrace();        }    }      // Driver method    public static void main(String[] args) throws Exception    {        // calling the        // serializeArrayList() method        serializeArrayList();    }}
Output: 
Note:All the elements stored in the ArrayList should also be serializable in order to serialize the ArrayList, else NotSerializableException will be thrown.
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?