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
Spring @Value annotation
In this post under Spring Core, I will show with example how to use “@Value” annotation. In previous post I showed how to access system properties, environment properties, custom properties using “Environment” interface. In all these case we had to explicitly retrieve the value as shown below and are not injected into the bean automatically…… Continue reading Spring @Value annotation
Iterating a list in both direction
In this post under Java, I will show with example how to iterate a list in both directions. For iterating in both direction, we will take the help of “ListIterator” class. We get an instance of “ListIterator” by calling “listIterator” method on a list. Below is the code snippet, where “integerList” is a list of…… Continue reading Iterating a list in both direction
Sorting a list
In this post under Java, I will show with example how to sort a list. Below is the complete main class for your reference Main class 1 package core.collection; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 public class SortingList { 8 public static void main(String []args) { 9 List<Integer> integerList…… Continue reading Sorting a list
assertThrows vs assertThrowsExactly example
In this post under JUnit, I will show with example the purpose of “assertThrowsExactly” method and how it is different with “assertThrows” method. I have explained with example, the purpose of “assertThrows” static method in previous posts. For recap, “assertThrows” method asserts that the test method throws the expected exception. If the test method doesn’t…… Continue reading assertThrows vs assertThrowsExactly example
IllegalRegexRule example
In this post under Passay, I will explain with example the purpose of “IllegalRegexRule” class. In one of my previous post, I have explained with example the purpose of “AllowedRegexRule” class. We will have recap of it here. When validating password, we mainly check whether the password follows a particular pattern. If the password follows…… Continue reading IllegalRegexRule example
beforeAll and afterAll annotation
In this post under JUnit, I will explain with example the purpose of “beforeAll” and “afterAll” annotations. In the previous post under JUnit, I covered about “beforeEach” and “afterEach” annotation. Similarly to “beforeEach” and “afterEach”, “beforeAll” and “afterAll” is applied to methods. But methods annotated with “beforeEach” and “afterEach” annotations are executed before and after…… Continue reading beforeAll and afterAll annotation