Posts

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

Java 8 Stream - Remove duplicates from a list of objects based on a property

In this section, we will show you how to remove duplicates from a list of objects based on a single property.  In this example, we are getting the stream from the list and putting it in the TreeSet from which we provide a custom comparator that compares id, email, or salary uniquely.   Here we are going to remove duplicates based on the,  id(Long) property of the user  email(String) property of the user  salary(Double) property of the user User.java public class User { private Long id ; private String name ; private String email ; private String phone ; private Double salary ; public Long getId () { return id ; } public void setId ( Long id) { this . id = id; } public String getName () { return name ; } public void setName ( String name) { this . name = name; } public String getEmail () { return email ; } public void setEmail ( String email) { this . email = email; }