Serialize /Convert Map of object to/from JSON in java (GSON /example)

Given the Map of String and object (Map<String,Object>), we would like to convert Map to JSON and vice versa. We will use the Google gson library to achieve serialization and deserialization process. We will create Person class and we will perform the following operations with Person class Convert Map<String,Person> to JSON string Convert the JSON string to Map<String,Person> GSON Maven… Read More »

Read a file line by line using java 8 lambda stream (example)

1. Read file using java 8 lambda stream Given a file, read input file by line by line using lambda stream in java 8. Get the input file (“readFile.txt”) from class path or some absolute location. We will use Java 7 feature try -with-resources, which will ensures that resources will be closed (automatically). Read the input… Read More »

Impact of interface’s default method on multiple inheritance (Java 8 /example)

Java 8 has introduced default method for interfaces, which provides the default implementation of method. Prior to java 8 interfaces does not have any default keyword There were no conflicts if class inherits multiple interfaces having same method (Prior to Java8). Example – multiple inheritance in java Let us take example to understand the multiple… Read More »

Java 8 Functional interface explained with examples (Comparator)

1. What is functional interface in java8? The interface having one abstract method is called functional interface. The interface can contains other default methods but needs to have one abstract method. The functional interface are generally marked with annotation “@FunctionalInterface”. Which signifies that they are functional interface. Having said that, It is not mandatory to… Read More »

Create runnable task using functional interface – java8 lambda stream (example)

1. Functional Interface java 8: The interface having one abstract method is called the functional interface. E.g. Comparator, Runnable interface. The runnable interface will look like as follows @FunctionalInterface public interface Runnable { public abstract void run(); } We have discussed about the Creating threads using runnable interface. In current post we will see how… Read More »

Serialization & Deserialization object in java using externalizable interface.

Given user defined object or POJO, we would like to serialize object in java. We will use Externalizable interface, so that class is marked for Serialization. Externalizable interface is sub interface of Serializable interface. We have discussed similar posts: Serialize user defined object using Serializable interface. Impact of inheritance on Serialization, when Base class is Serialization. Impact… Read More »