Searching an array for a particular element

In this post under Java Collections, I will show with example how to search an array for a specific element. Please note there is a precondition that must be satisfied before searching an array. The precondition is that the array should already be sorted. Java “Arrays” class provides “binarySearch” method which does the searching. This…… Continue reading Searching an array for a particular element

Arrays to List conversion

In this post under Java Collections, I will explain with example how to convert an array to list. Please note this conversion is possible if the array is a collection of objects and not primitive elements. To achieve our goal, we use “Arrays” class static method “asList”. This method returns a list backed by the…… Continue reading Arrays to List conversion

Mapping between objects with default value

In this post under MapStruct, I will show with example how to map two objects with default value. Let me further elaborate the requirement. We have “Student” class and we have to map the instance of “Student” class to an instance of “StudentDTO” class. During mapping if the value of “className” attribute in “Student” class…… Continue reading Mapping between objects with default value

Checking whether the password is already used in your application

In this post under Passay, I will show with example how to check whether a user entered password is already used in the application or not. Below is the complete main code for your reference Main class 1 package defaultPackage; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.passay.HistoryRule; 7 import org.passay.PasswordData; 8…… Continue reading Checking whether the password is already used in your application

Converting Collection to unmodifiable Set

In this post under Java, I will show with example how to convert a collection to unmodifiable set. To convert a collection to an immutable Set, we take help of “Set” interface static method “copyOf”. Below is the complete code for your reference. Main class 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.List;…… Continue reading Converting Collection to unmodifiable Set

Converting Collection to unmodifiable List

In this post under Java, I will show with example how to convert a collection to unmodifiable list. To convert a collection to an immutable List, we take help of “List” interface static method “copyOf”. Below is the complete code for your reference. Main class 1 package core.collection; 2 3 import java.util.HashSet; 4 import java.util.List;…… Continue reading Converting Collection to unmodifiable List

Mapping from multiple objects to one destination object

In this post under MapStruct, I will explain with example how to map attributes from multiple objects to attributes of one single destination object. For our example I will two source class as shown below Person package package4; public class Person { private int id; private String name; private int age; private char sex; //Removed…… Continue reading Mapping from multiple objects to one destination object