Skip to content
Related Articles

Related Articles

How to Serialize ArrayList in Java?
  • Last Updated : 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.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :