Serialize and Deserialize an ArrayList in Java

Learn to serialize and/or deserialize an ArrayList in Java with easy-to-follow examples. Note that the list items must also be Serializable.

1. Serialization

In Java, ArrayList class is serializable by default. It essentially means that we do not need to implement Serializable interface explicitly to serialize an ArrayList. We can directly use ObjectOutputStream to serialize ArrayList, and ObjectInputStream to deserialize an arraylist object.

The elements stored in arraylist should also be serializable, else program will throw NotSerializableException.

1.1. Serialize ArrayList of Strings

Given below is an example Java program to persist an arraylist of strings.

ArrayList<String> namesList = new ArrayList<>(List.of("alex", "brian", "charles"));

try (FileOutputStream fos = new FileOutputStream("listData");
    ObjectOutputStream oos = new ObjectOutputStream(fos);) {

  oos.writeObject(namesList);

} catch (FileNotFoundException e) {
  log.error("File not found : ", e);
  throw new RuntimeException(e);
} catch (IOException e) {
  log.error("Error while writing data : ", e);
  throw new RuntimeException(e);
}

Program output.

The list is serialized in the project root folder. Provide the file path in the FileOutputStream constructor.

ArrayList Serialized
ArrayList Serialized

2. Serialize ArrayList of Objects

Given below is a Java program to serialize an ArrayList of Employee objects. Employee class implements the Serializable interface.

@Data
@AllArgsConstructor
@NoArgsConstructor
class Employee implements Serializable {

  private Long id;
  private String firstName;
  private String lastName;
}
ArrayList<Employee> employees = new ArrayList<>();

employees.add(new Employee(1L, "lokesh", "gupta"));
employees.add(new Employee(2L, "brian", "motto"));

try (FileOutputStream fos = new FileOutputStream("employeeData");
    ObjectOutputStream oos = new ObjectOutputStream(fos);) {

  oos.writeObject(employees);

} catch (FileNotFoundException e) {
  log.error("File not found : ", e);
  throw new RuntimeException(e);
} catch (IOException ioe) {
  log.error("Error while writing data : ", ioe);
  ioe.printStackTrace();
}

Notice if Employee class does not implement Serializable interface, we will receive this error.

java.io.NotSerializableException: com.howtodoinjava.demo.model.Employee
	at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
	at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)

2. Deserialization

2.1. Deserialize List of Strings

Java program to deserialize a list of strings and verify list content. We are deserializing the same list, we serialized in the previous example.

ArrayList<String> list = null;

try (FileInputStream fis = new FileInputStream("listData");
    ObjectInputStream ois = new ObjectInputStream(fis);) {

  list = (ArrayList) ois.readObject();
} catch (IOException ioe) {
  ioe.printStackTrace();
} catch (ClassNotFoundException c) {
  System.out.println("Class not found");
  c.printStackTrace();
}

//Verify list data
for (String name : list) {
  System.out.println(name);
}

Program output.

alex
brian
charles

2.2. Deserialize a List of Objects

Java program to deserialize a list of objects and verify list content. We are deserializing the same list of employees, we serialized in the previous example.

ArrayList<Employee> employeesList = null;

try (FileInputStream fis = new FileInputStream("employeeData");
    ObjectInputStream ois = new ObjectInputStream(fis);) {

  employeesList = (ArrayList) ois.readObject();

} catch (IOException ioe) {
  ioe.printStackTrace();
} catch (ClassNotFoundException c) {
  System.out.println("Class not found");
  c.printStackTrace();
}

//Verify list data
for (Employee employee : employeesList) {
  System.out.println(employee);
}

Program output.

Employee [id=1, firstName=lokesh, lastName=gupta]
Employee [id=2, firstName=brian, lastName=motto]

Happy Learning !!

Read More: ArrayList Java Docs

Sourcecode on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.