Skip to main content

Posts

Showing posts from March, 2020

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…

How to synchronize ArrayList in Java

How to synchronize ArrayList in Java? In this article, we are going to learn how we can synchronize ArrayList in Java. As we know, ArrayList in Java is not thread-safe Collection i.e., not synchronized. Why ArrayList is not Synchronized? First, we should know "Why ArrayList is not Synchronized?" before knowing how to make that synchronized. Most of the real-world scenarios need thread-based implementation plus ArrayList can be used as a better alternative to an Array. Hence if ArrayList is synchronized we can easily use ArrayList in the multi-threaded environment. So the question is, Why it is not Synchronized?
The answer lies in terms of the performance aspect. To make any class synchronized, it needs performance overhead. If the ArrayList would be made as synchronized, Each-n-Every operation onto the ArrayList will take some extra time as compared to the current implementation of the ArrayList and, the ArrayList is used widely in the java so the developer of the ArrayList …

Serialize ArrayList in java

How to serialize ArrayList in Java In this article, we are going to learn how to serialize ArrayList in Java. Also, we will see how to deserialize ArrayList in Java. To serialize or deserialize, the class must implement the java.io.Serializable interface. In our case, we can serialize ArrayList as ArrayList class implements Serializable Interface, See the hierarchy of ArrayList. "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 ArrayList of strings?How to serialize ArrayList of user-defined objects?
How to serialize ArrayList of strings? Let's start with a very basic example to serialize ArrayList of string objects. We can use the w…

Java subList from ArrayList

How to get subList from ArrayList in Java? In this article, we are going to learn how we can get a sublist from ArrayList in Java. By applying the starting and ending index, we can find a sublist from ArrayList. Also, we are going to see how to find a sublist in java based on some predicate.

If you are new to ArrayList, read this article first: All about ArrayList in Java.
Content of the ArticleJava sublist by starting and ending indexJava sublist by Predicate
Java sublist by starting and ending index The subList() method of ArrayList is used to find a sublist in java. This method returns a portion of an ArrayList based on the starting and ending index. Let's first look at the syntax of subList(): Syntax of subList() List<T> subList = arrayList.subList(fromIndex, toIndex); This method takes two integer arguments: fromIndex and toIndex as name suggests, fromIndex for starting index and toIndex for ending index. The toIndex is an exclusive type argument i.e., the element of Array…

Java ArrayList Common Program Examples

Java ArrayList Common Program Examples In this article, we are going to learn common programs related to Java ArrayList that help in our day-to-day programming activities. ArrayList is a very important class in Java and used extensively in our programs. Therefore, it is good to learn common tasks related to ArrayList so that we can solve easily our day-to-day programming challenges around the ArrayList.
If you are new to ArrayList and want to learn the basics of ArrayList, I suggest you read this article first: All about ArrayList in Java. Now, let's look at the common programs related to ArrayList.
Content of the ArticleHow to initialize ArrayList in Java?How to iterate ArrayList in Java?How to convert ArrayList to Array in Java?How to convert ArrayList to HashMap in Java?How to sort ArrayList in Java?How to remove duplicates from ArrayList in Java?How to compare two ArrayList in Java?How to serialize ArrayList in Java?
How to initialize ArrayList in Java? To use an ArrayList in …

Different ways to initialize ArrayList in Java

How to initialize ArrayList in Java In this article, we are going to learn different ways to initialize ArrayList in Java. If you don't know about Arraylist, read this article to know All about the ArrayList in Java.
Content of the ArticleHow to initialize ArrayList in Java - using Default Constructor?How to initialize ArrayList in Java - using Copy Constructor?How to initialize ArrayList in Java - using Arrays.asList() method?How to initialize ArrayList in Java - using stream API?
How to initialize ArrayList in Java - using Default Constructor? We can initialize ArrayList in Java using default constructor and then we can add elements into the ArrayList with the help of add() method. For Example Syntax ArrayList<T> arrayList = new ArrayList<>(); Java init ArrayList Example 1: Initialize ArrayList in Java - using Default Constructorimport java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<Stri…