Serialization and deserialization in java

How to Serialization and Deserialization of HashMap in Java

Welcome to ThoughtCoders Java tutorial. In this article, we will learn about what is Serialization and Deserialization, why it’s requires and how to serialize HashMap. This tutorial is self-explanatory and each step is explained in details and with a code snippet.

What is serialization and de serialization in Java and how to serialize/deserialize HashMap in Java

What is Serialization: Serialization is the process of converting an object into a byte stream and which can be further stored in Memory (on Disk) or sent to another network (or environment) to read and utilize that data.      

Or Serialization refers to arranging something in sequence. In a similar way, Serialization in Java transferred objects stream into bits. Transformation maintained by supplied metadata.

Serialization is the process of converting Java object into files.
Serialization in Java

What is Deserialization:  Deserialization is the reverse process of serialization. In this process already serialized file converted into Java object and further processed in program.

Deserialization is the reverse process of serialization
Deserialization in Java

When and Why we do serialization and Deserialization: While Java program execution multiple objects created and destroyed by Garbage Collector. But we want to store a few objects as a file on local machine or network for this serialization is the best option.

  1. Serialization and Deserialization are platform-independent processes. If one object serialized on one machine can be deserialized on a different machine.
  2. Serialization gives us flexibility store object state during execution.

Pre requisite :

  1. Java should be installed ( If Java is not installed then refer our step by step tutorial (Link)
  2. Java Project should be created.

Steps to Serialize HashMap in Java: Any class which implements serializable can be serialized. HashMap class is already serialized so we can directly serialize HashMap.

Step 1: Open Java Project and create one Class with main method

Step 2: Make one HashMap object and add values in key and pair form.

package serializeDemo;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
public class hashmapSerialize {
	public static void main(String[] args) {
	HashMap<String, String>  data= new HashMap<String, String>();
	data.put("Name", "Aniket");
	data.put("Class", "B.Tech");
	data.put("Subject", "CSE");
	data.put("Address", "Lucknow");
	System.out.println("Data to be saved: "+data);
	
	}
}

Step 3: Create FileOutputStream Object to write byte data on disk and pass file name “data.ser” to write.

FileOutputStream fos = new FileOutputStream(“data.ser”);

Step 4: Make ObjectOutputStream object to write hashmap data into the stream.

ObjectOutputStream oos = new ObjectOutputStream(fos);

Step 5: Use ObjectOutputStream object and use method writeObject to write HashMap and the close ObjectOutputStream and flush FileOutputStream object.

oos.writeObject(data);
oos.close();
fos.flush();

Complete code of HashMap Serialization:

package serializeDemo;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
public class hashmapSerialize {
	public static void main(String[] args) {
	HashMap<String, String>  data= new HashMap<String, String>();
	data.put("Name", "Aniket");
	data.put("Class", "B.Tech");
	data.put("Subject", "CSE");
	data.put("Address", "Lucknow");
	System.out.println("Data to be saved: "+data);
	try {
	FileOutputStream fos = new FileOutputStream("data.ser");
	ObjectOutputStream oos = new ObjectOutputStream(fos);
	oos.writeObject(data);
	oos.close();
	fos.flush();
	System.out.println("Output is saved in data.ser");
	}catch(Exception e) {
		e.printStackTrace();
	}
	}
}
Serialized Hashmap in Java
Serialized Hashmap in Java
Object converted into data file and store in Memory

Steps to Deserialize HashMap: Using the above steps we stored HashMap into a file. Now we want to read. Deserialization is the reverse process of serialization. Here is the step by step guide.

Step1: Create one class with Main Method

Step 2: To read file, create FileInputStream Object and pass file name as constructor.

FileInputStream fis = new FileInputStream("data.ser");

Step 3: Create object of ObjectInputStream to serialize object

ObjectInputStream ois = new ObjectInputStream(fis);

Step 4: Create HashMap object to store values of deserialize HashMap and ObjectInputStream method readObject().

map = (HashMap)ois.readObject();
		System.out.println("Deserialized HashMap "+map);

Complete Code:

package serializeDemo;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.HashMap;
public class deserializeDemo {
	public static void main(String[] args) {
		try {
		FileInputStream fis = new FileInputStream("data.ser");
		ObjectInputStream ois = new ObjectInputStream(fis);
		HashMap<String, String> map = new HashMap<String, String>();
		map = (HashMap)ois.readObject();
		System.out.println("Deserialized HashMap "+map);
		} catch(Exception e) {
			
			e.printStackTrace();
		}
	}
}

Console Output:

Deserialized Hashmap in Java
Deserialized Hashmap in Java
Console output – Deserialized Output on console

Congratulations! You have successfully serialized HashMap and also deserialized. Hope you followed our steps and learnt. Still, if you face any issue then feel free to contact us on info@thoughtcoders.com or call us on +919555902032.

 

Thoughtcoders is the leading Software Testing Company which offers end to end Software Testing Services. Our major services are end to end testing services for businesses, regression testing, test automation, non functional testing, performance testing and accessibility testing. We are working on Automation First strategy by utilising the open source technologies. If  you are looking for Software Testing Services then you may write us on info@thoughtcoders.com

Write A Comment