In this post under Java Collections, I will explain with example the purpose of “nCopies” static method in “Collections” utility class This method creates a list containing n copies of input object. Where n and input object are user specified. So basically this method takes two arguments.1) The object to be replicated in the list2)…… Continue reading List nCopies demo
MappingConstants.NULL example
In this post under MapStruct, I will explain with example the purpose of MappingConstants.NULL enum constant. In all my previous posts, I explained how to map a source enum constant to destination enum constant. What if the client calling the code (that will map passed source enum constant to destination enum constant) passes a null…… Continue reading MappingConstants.NULL example
Elvis operator
In this post under Spring SpEl, I will explain with example the purpose of Elvis operator. Lets say you have below statement written using ternary operator String result = (data != null) ? data : null; In the above statement “data” variable is of type String. So we are checking if “data” is not null,…… Continue reading Elvis operator
List rotation demo
In this post under Java Collections, I will explain with example how to rotate elements in a list. Java “Collections” utility class has static method named “rotate” which will rotate elements in a list. This method takes two arguments.1) the list2) integer representing the distance Below is the complete main class for your reference Main…… Continue reading List rotation demo
MappingConstants.THROW_EXCEPTION example
In this post under MapStruct, I will explain with example the purpose of enum “MappingConstants.THROW_EXCEPTION”. This enum is mainly used when mapping source enum constant to destination enum constant. If you want to throw an exception for some source enum constant. We can use this enum constant. Below is the example showing the usage. Lets…… Continue reading MappingConstants.THROW_EXCEPTION example
Safe navigation operator
In this post under Spring SpEl, I will explain with example the usage of safe navigation operator. Lets say we have below pojo class Employee package spel.package24; public class Employee { private Integer id; private String name; private int salary; public Employee() { } public Employee(Integer id, String name, int salary) { this.id = id;…… Continue reading Safe navigation operator
String to InputStream conversion
In this post under Apache Commons IO, I will show with example how to convert a String to InputStream. “IOUtils” class in Apache Commons IO framework provides a method named “toInputStream” which converts a String to InputStream. This method takes two arguments1) instance of String class2) Charset type Below is the main class showing how…… Continue reading String to InputStream conversion
Using @Singular with @Builder annotation example
In this post under Lombok I will explain with example the purpose of “@Singular” annotation which is alwayse used with “@Builder” annotation. In previous post under lombok I have explained why we use “@Builder” annotation i.e., to implement builder pattern. So if have below pojo class with “@Builder” annotation at the class level Pojo class…… Continue reading Using @Singular with @Builder annotation example
Getting URL of classpath resource
In this post under Apache Commons IO, I will show with example how to get URL of a resource present in application classpath. “IOUtils” class provides a static method “resourceToURL” which takes the resource name and the classloader as arguments and returns url of the resource. Below is the main class showing how to use…… Continue reading Getting URL of classpath resource
Using @Builder annotation example
In this post under Lombok, I will show with example how to use “@Builder” annotation to implement Builder Pattern in our application. For our example, we use the below Person pojo class. Person class package package26; public class Person { private String name; private String city; @Override public String toString() { StringBuilder stringBuilder = new…… Continue reading Using @Builder annotation example