Skip to main content

Posts

Showing posts from April, 2020

Serialization in Java

Serialization in Java In this article, we are going to learn how to accomplish Serialization in java and the importance of serialization. Also, we will learn the reverse of serialization i.e., Deserialization. What is Serialization in java? Serialization is a process that is used to convert an object into the stream of bytes so that the stream data bytes can be transferred to any location outside the java like databases, network transmission, files, etc. Deserialization in java Deserialization is the reverse of serialization in which the stream of bytes is converted back into an object. This is majorly used to fetch data from databases, read files or received data packets from the network and deserialized into an object within the java memory space. How to do serialization in java? To achieve serialization in java, we need to use a marker interface named as Serializable. Serializable interface is the part of the java.io package and If any object needs to be serialized or deserialized …

Serialize HashSet in Java

How to serialize hashset in Java In this article, we are going to learn how to serialize HashSet in Java. Also, we will see how to deserialize HashSet in Java. To serialize or deserialize, the class must implement the java.io.Serializable interface. In our case, we can serialize HashSet as HashSet class implements Serializable Interface, See the hierarchy of HashSet. "Serialization is a process in which we convert the object into the byte stream and this stream of bytes can be stored into the file system/databases or can be placed onto the network for transmission from one place to another.
Deserialization is the reverse process of the Serialization. Deserialization is to get back the objects from the byte stream."
Table of Content1. How to serialize HashSet of strings?2. How to serialize HashSet of user-defined objects?
How to serialize HashSet of strings? Let's start with a very basic example to serialize HashSet of string objects. We can use the writeObject() method …

Different ways to iterate HashSet in java

How to Iterate HashSet in java In this article, we are going to learn about how to iterate HashSet in java. Java provides various ways to loop over the HashSet that we are going to see one-by-one. What is HashSet in java? HashSet in java is an implementation of the Set interface of Java. A Set is similar to a List but duplicate elements are not allowed in a Set. To know more about the HashSet, visit this article: HashSet in java
Table of Content1. How to iterate HashSet in java using Iterator?2. How to iterate HashSet in java using for each loop?3. How to iterate HashSet in java using forEach() method? How to iterate HashSet in java using Iterator? HashSet provides a method called iterator() which returns an object of Iterator. Iterator is an interface defined in java.util package and its implementation are provided by the HashSet internally.
Look at the Syntax of how to get the object of Iterator from the HashSet: Syntax of iterator() method Iterator<T> iterator = hashSet.iterato…

How to synchronize HashSet in java

How to synchronize HashSet in java? In this article, we are going to understand how we can synchronize HashSet in Java. As we know, HashSet is not synchronized in java i.e., it is not thread-safe. So we must know how to make a HashSet synchronized so that we can use it in the multi-threaded environment. Why HashSet is not Synchronized? First, we should know "Why HashSet is not Synchronized?" before knowing how to make that synchronized. HashSet is a widely used Collection whenever we need to use an implementation of the Set interface. If HashSet would be made as synchronized then it would be much slower as compared to the current implementation of HashSet. Because to make any class synchronized, it needs performance overhead.
Therefore, the developers of HashSet made it as non-synchronized to work faster and provide other ways to make it synchronized.
There are two ways to synchronize HashSet in Java: Using Collections.synchronizedSet() method Using CopyOnWriteArraySet cla…