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
@RepeatedTest annotation
In this post under JUnit, I will show with example the purpose and how to use JUnit 5’s “RepeatedTest” annotation. The “RepeatedTest” annotation is applied on a method. Once applied it indicates the JUnit that it has repeat testing of annotated method a specified number of times. Below is the snapshot showing how to use…… Continue reading @RepeatedTest annotation
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
assertIterableEquals example
In this post under JUnit, I will show with example how to assert whether a expected collection has same elements, at the same position when compared to actual collection. Pre JUnit 5, to do this,1) we have to iterate item by item in both collection (expected and actual) at the same time2) compare the items…… Continue reading assertIterableEquals example
Clearing ObjectPool of Idle objects
In this post under Apache pool, I will show with example how to clear idle objects from the pool. For our example lets create a pool with 5 objects and then make 3 out of them idle. I will then show how to clear those 3 idle objects. Below is the complete main code for…… Continue reading Clearing ObjectPool of Idle objects