How to Serialize ArrayList in Java? Last Updated : 15 Dec, 2020 Comments Improve Suggest changes Like Article Like Report 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. Comment More infoAdvertise with us Next Article How to Serialize ArrayList in Java? ushashree Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads How to make ArrayList Thread-Safe in Java? In Java, Thread is the smallest unit of execution within the program. It represents an independent path of execution that can run concurrently with other threads. When dealing with multi-threaded applications, where multiple threads are accessing and modifying data concurrently, it's crucial to ensu 3 min read How to Serialize and Deserialize a HashSet in Java? Serialization is the technique of converting an object's state into a byte stream. The main purpose of serialization is to save the state of an object so that it can be rebuilt later. In Java, a Serializable interface is used to mark classes as serializable. When a class implements the Serializable 3 min read How to Efficiently Serialize and Deserialize Arrays in Java? Serialization is the process of converting an object into a byte stream and deserialization is the process of reconstructing the object from that byte stream. When working with the Arrays in Java, efficiently Serializing and Deserializing them is essential for data storage and transfer. Serialize an 2 min read How to Serialize and Deserialize a TreeMap in Java? In Java, serialization is implemented by using the Serializable Interface. The use of the TreeMap class is a simple way to serialize and deserialize the objects. It is a component of the Java Collections framework. For writing and reading objects, we will be using the ObjectOutputStream and ObjectIn 2 min read How to Add Element in Java ArrayList? Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. Element can be added in Java ArrayList using add() method of java.util.ArrayList class. 2 min read How to Declare an ArrayList with Values in Java? ArrayList is simply known as a resizable array. Declaring an ArrayList with values is a basic task that involves the initialization of a list with specific elements. It is said to be dynamic as the size of it can be changed. Proceeding with the declaration of ArrayList, we have to be aware of the co 2 min read Convert ArrayList to Vector in Java There are several ways to convert ArrayList to Vector. We can use a vector constructor for converting ArrayList to vector. We can read ArrayList elements one by one and add them in vector. Approach 1: (Using Vector Constructor) Create an ArrayList.Add elements in ArrayList.Create a vector and pass t 3 min read How to Clone a List in Java? Given a list in Java, the task is to clone this list. Example: Input: list = ["Geeks", "for", "Geeks"] Output: clonedList = ["Geeks", "for", "Geeks"] Input: list = ["GeeksForGeeks", "A Computer Science", "Portal"] Output: clonedList = ["GeeksForGeeks", "A Computer Science", "Portal"] In Java, there 9 min read Convert ArrayList to Comma Separated String in Java ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows: Earlier before Java 8 there were 5 min read Like