How to Serialize and Deserialize a HashSet in Java?
Last Updated :
24 Apr, 2025
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 interface the instances can be converted into a sequence of bytes.
Implementing the Serialize and Deserialize HashSet in Java
Serialization
It can be implemented using the FileOutputStream and ObjectOutputStream these can be used to implement the serialize and deserialize into the Java program. The following two packages play a significant role in the operations of the Java program.
- FileOutputStream: It is the pre-defined class in Java that can be used to write data to a file as a stream of bytes. It is a part of the java.io package.
- ObjectOutputStream: It is also a pre-defined class in Java that can be used to write the objects to an outputStream and it permits the serialize objects into a stream of bytes.
Below is the implementation For Serialization in HashSet:
Java
// Java Program to implementation
// For Serialization in HashSet
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashSet;
// Driver Class
public class HashSetSerialization {
// Main Function
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hashSet = new HashSet<>();
hashSet.add("Java");
hashSet.add("Python");
hashSet.add("C#");
// Serialize the HashSet
try (FileOutputStream fileOut =
new FileOutputStream("C:/Users/Mahesh/Desktop/DSAˍPratice/windowsliding/hashSet.ser");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
objectOut.writeObject(hashSet);
System.out.println("HashSet has been serialized.");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
HashSet has been serialized.
Explanation of the above Program:
In the above program, there is an example of the serialization of the HashSet and after generates the serialize file hashSet.ser file can be saved in your local file system, and it can be implemented by first creating the instance of the hash set, then adding the elements to the hash set, and then serializing the hash set using the created instance of the FileOutputStream. Using writeObject() method can be used to convert the HashSet into the Serialize file.
Deserialization
In Java, Deserialization is the technique of the converting the serialize objects into the in-memory objects. We already know that, serialization is the technique of converting the object into a byte stream, which can be saved as a file. Deserialization is the reverse process.
Below is the implementation For Deserialization in HashSet:
Java
// Java Program to implementation
// For Deserialization in HashSet
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashSet;
public class HashSetDeserialization {
public static void main(String[] args) {
// Deserialize the HashSet
try (FileInputStream fileIn =
new FileInputStream("C:/Users/Mahesh/Desktop/DSAˍPratice/windowsliding/hashSet.ser");
ObjectInputStream objectIn = new ObjectInputStream(fileIn)) {
// Read the object from the stream
// and cast it to HashSet
HashSet<String> deserializedHashSet = (HashSet<String>) objectIn.readObject();
// Print the elements of the deserialized HashSet
System.out.println("Deserialized HashSet: " + deserializedHashSet);
}
catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}