Posts

Learn Java Predicate, Consumer, Function, and Supplier (Functional Interfaces) with Example

Java is Object Oriented Programming language, being object-oriented is not bad, but it brings a lot of verbosity to the program.Java 8 introduced new libraries and programming styles, for example, functional interfaces, lambda expressions, and streams. These bring functional-style programming to the object-oriented programming capabilities of Java. Java Functional Interface and Lambda Expression help us write smaller and cleaner code by removing much boilerplate code.  A functional interface contains only a single abstract method and  can include default and static methods that do have an implementation and a single unimplemented method. Here we will discuss Predicate, Consumer, Function, and Supplier. 1. Predicate Interface The Predicate is a functional interface defined in java. util. Function package , which accepts an argument and returns a boolean. This is mainly used to filter data from a Java Stream. The filter method of a stream accepts a predicate to filter the data and retur

Java this keyword - Example

The ' this ' keyword in Java is a keyword that can be used inside a class method or constructor. The 'this' keyword works as the reference to the current object, whose method or constructor is being invoked. You can use the 'this' keyword to reference any member of the current object inside an instance method or constructor. Following are the ways to use the ' this ' keyword in java: 1. Using ' this ' keyword to refer to current class instance variables 2. Using  this () to invoke the current class constructor 3. Using  ' this '  keyword to return the current class instance 4. Using  ' this '  keyword as a method parameter 5. Using  ' this '  keyword to invoke the current class method 6. Using  ' this '  keyword as an argument in the constructor call Using 'this' keyword to refer to current class instance variables //Java code for using 'this' keyword to //refer current class instance variables publ

5 ways to convert JSON to Java Object - Using Jackson, Gson, Genson, Moshi, & FastJson

Image
Hello everyone, today we will show you how to convert JSON to Java Object using five different Java libraries which are listed below. 1. Jackson 2. Gson 3. Moshi 4. Genson 5 FastJson In this example, we are going to convert the below JSON string to Java Object, Sample JSON String public class Json { public static String json = "" + "{\"isbn\": \"123-456-222\", \n" + " \"author\": \n" + " {\n" + " \"lastname\": \"Doe\",\n" + " \"firstname\": \"Jane\"\n" + " },\n" + "\"editor\": \n" + " {\n" + " \"lastname\": \"Smith\",\n" + " \"firstname\": \"Jane\"\n" + " },\n" + &quo