How to Serialize and Deserialize a HashSet in Java?
Last Updated :
28 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();
}
}
}
Output:
Deserialized HashSet: [Java, Python, C#]
Explanation of the above Program:
The above program is an example of the deserialization of the hashset in the Java program. It can be implemented by first creating an instance of the FileInputStream, then accessing the hashSet.ser file. After that, using the instance of the ObjectInputStream then read the seralize.ser file using readObject() method then adding deserialize elements into the HashSet then print the HashSet.
Similar Reads
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 Serialize ArrayList in Java? 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 method
2 min read
How to Convert a HashSet to JSON in Java? In Java, a HashSet is an implementation of the Set interface that uses a hash table to store elements. It allows fast lookups and does not allow duplicate elements. Elements in a HashSet are unordered and can be of any object type. In this article, we will see how to convert a HashSet to JSON in Jav
2 min read
How to Convert ArrayList to HashSet in Java? ArrayList: In Java, ArrayList can have duplicates as well as maintains insertion order. HashSet: HashSet is the implementation class of Set. It does not allow duplicates and uses Hashtable internally. There are four ways to convert ArrayList to HashSet : Using constructor.Using add() method by itera
3 min read
How to Convert Comma Separated String to HashSet in Java? Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which
2 min read