How to Serialize and Deserialize ArrayList in Java with Example

In this post, we will study how to serialize and deserialize an ArrayList object. One important point to note about ArrayList is that it is serializable by default. So in other words, you do not need to implement Serializable interface explicitly in order to serialize an ArrayList.

Read Also: What is Serialization in Java

I have already shared how to synchronize an ArrayList in java. Let's start with the example of serialization before moving on to the example of deserialization.

Serialization and Deserialization of ArrayList in Java

1. Serialization of ArrayList


When you run the below example then it will create a file named inputfile. The inputfile  will be having ArrayList object in the form of stream of bytes. One important point to note that we would be using inputfile at the receivers end to recreate the Object from stream of bytes.

Note: We do not need to implement the Serializable interface in the below example because ArrayList is serialized by default.


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

public class ArrayListSerialization {
    public static void main(String args[]) {
      // ArrayList object creation
      ArrayList<String> list = new ArrayList();
      
      // Adding elements to the ArrayList object 
      list.add("Boston");
      list.add("Dallas");
      list.add("New York");
      
      try {
         FileOutputStream fileOS = new FileOutputStream("inputfile");
         ObjectOutputStream objOS = new ObjectOutputStream(fileOS);
         objOS.writeObject(list);
         objOS.close();
         fileOS.close();
      } 
      catch(IOException ex) {
         ex.printStackTrace();          
      }
    }
}

2. Deserialization of ArrayList


In the below example, we are deserializing the object. In other words, we are retrieving the stream of bytes from input file which we have stored using the above example. Then, we are typecasting the returned object to ArrayList and showing the elements of ArrayList.

Note: In the output of deserialization, we are getting the same elements which are added to the ArrayList during serialization.


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

public class ArrayListDeserialization {
    public static void main(String args[]) {
      // ArrayList instantiation
      ArrayList<String> list = new ArrayList();
      
      try {
         FileInputStream fileIS = new FileInputStream("inputfile");
         ObjectInputStream objIS = new ObjectInputStream(fileIS);
         list = (ArrayList) objIS.readObject();
         objIS.close();
         fileIS.close();
      } 
      catch(IOException ex) {
         ex.printStackTrace(); 
         return;
      }
      catch(ClassNotFoundException ex2) {
         System.out.println(" Class Not Found Exception");
         ex2.printStackTrace();
         return;
      }
      // Printing the ArrayList
      for(String s : list) {
        System.out.println(s);
      }    
    }
}


Output:

Boston
Dallas
New York


That's all for today. Please mention in the comments if you have any questions related to how to serialize and deserialize ArrayList in Java with example.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry