javaserializationlinked-listdeserialization

Proper way to Serialize/Deserialize a custom linked list in Java


I'm adding serialization to my database project and I'm having problems understanding how to deserialize my linked list. I think I serialized it correctly, but I would like feedback on my implementation there as well as I'm not entirely sure it's the correct way to do it.

My custom linked list class Enrollment:

/*
class which is used to create the
enrollment linked list referencing
Student and Course objects
*/
public class Enrollment implements Serializable
{
    private Student student;
    private Course course;
    private Enrollment link;

    public Enrollment(Student student, Course course)
    {
        this.student = student;
        this.course = course;
        this.link = null;
    }

    //returns student object to caller
    public Student getStudent()
    {
        return student;
    }

    //sets student field
    public void setStudent(Student student)
    {
        this.student = student;
    }

    //returns course object to caller
    public Course getCourse()
    {
        return course;
    }

    //sets course field
    public void setCourse(Course course)
    {
        this.course = course;
    }

    //returns link to caller
    public Enrollment getLink()
    {
        return link;
    }

    //sets link field
    public void setLink(Enrollment link)
    {
        this.link = link;
    }

}//end Enrollment

For serialization, I have an object reference to the front of the list called allEnrollment. I figured that serializing only this reference would not serialize the entire list but rather just the first node. Here is my method for serializing the linked list (please correct me if this is not how it should be done):

void saveEnrollment(String filename) throws IOException
    {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
        Enrollment currNode = allEnrollment;

        //iterating thru linked list and writing each object to file
        while (currNode != null)
        {
            out.writeObject(currNode);
            currNode = currNode.getLink();
        }

        out.close();
    }

Assuming my saveEnrollment method is correct for serialization, how would I go about deserializing this linked list properly? I'm struggling badly and could use some advice. All the reading I've done has left me more confused. All members of Enrollment implement Serializable, so I should be good there.

Edit

Here is the deserialization method I've added from the great advice below, in case anyone else wants to see it for future reference:

    void loadEnrollment(String filename) throws ClassNotFoundException, IOException
    {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
        allEnrollment = (Enrollment)in.readObject();
        
    }

Solution

  • You don't have to do anything. As long as the Enrollment and Student classes are Serializable, serializing the head of the list will serialize the entire list, and deserializing it will recover the entire list.

    void saveEnrollment(String filename) throws IOException
    {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
        out.writeObject(allEnrollment);
        out.close();
    }