Skip to main content

Posts

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 …
Recent posts

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…

LinkedList in Java

LinkedList in Java In this article, we are going to learn about one more class that implements a linear data structure which is LinkedList in java (do not confuse this article with Linked List Data Structure, this article is all about the LinkedList class provided by the Java Collection Framework). We all have encountered a few instances where we need to use a Linked-List data structure so it is important to understand a pre-developed class provided by the Java Collection Framework that implements this data structure. In this post, we start with what is LinkedList in Java, its hierarchy, methods provided by the LinkedList in java and, etc.
Table of Content1. What is LinkedList in Java?2. Hierarchy of the LinkedList in java3. Internal Working of LinkedList in Java4. How to instantiated LinkedList in java?5. How to add elements into LinkedList in java?6. How to iterate LinkedList in Java?7. How to get elements from the LinkedList in java?8. How to get the index of given elements from th…

Serialize HashMap in Java

How to serialize HashMap in Java In this article, we will learn about how to serialize HashMap in java. To serialize anything, it must implement java.io.Serializable interface and HashMap does implement this interface. Also, we are going to learn how to deserialize HashMap in java.
If you are new to HashMap, I suggest you to read first All about HashMap in Java. "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."
Content of the ArticleHow to serialize HashMap of string keys and values?How to serialize HashMap of user-defined object values?
How to serialize HashMap of string keys and values? Let's start with a very basic example to serialize HashMap of string keys and values…

How to synchronize HashMap in Java

How to synchronize HashMap in Java? In this article, we are going to learn how we can synchronize HashMap in Java. As we know, HashMap is not synchronized in java i.e., it is not thread-safe. So we must know how to make HashMap synchronized to use it in the multi-threaded environment. Why HashMap is not Synchronized? First, we should know "Why HashMap is not Synchronized?" before knowing how to make that synchronized. HashMap is used in almost all real-world scenarios and if HashMap is synchronized then it would be much slower as compared to the current implementation. Because to make any class synchronized, it needs performance overhead.
Therefore, the developers of HashMap made it as non-synchronized to work faster and provide other ways to synchronize HashMap.
There are two ways to synchronize HashMap in Java: Using Collections.synchronizedMap() method Using ConcurrentHashMap class
Content of the ArticleHow to synchronize HashMap in Java - Using Collections.synchronize…