![]() |
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 JavaSerializationIt 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.
Below is the implementation For Serialization in HashSet:Java
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. DeserializationIn 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
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. |
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |