This post explains how to verify that a method is executed desired number of times using Mockito framework. Below is the code of the class to be tested. Class1 code package package14; import java.util.List; public class Class1 { private List list; public void method1() { for(int i = 0; i < 10 ;i++) { list.add(i);…… Continue reading Verifying the exact number of method invocations
Month: August 2018
JCache EntryProcessor Example
This post introduces you to JCache EntryProcessor interface. This interface is used to create concrete classes, whose purpose is to modify a cache entry. This is useful when multiple thread are accessing the cache and trying to modify a cache entry at the same time. To avoid threading issues like race condition etc, the thread…… Continue reading JCache EntryProcessor Example
Sorting arrays using Comparator
This post explains how to sort arrays using comparator with an example. The java jdk provides Arrays class, which has a static method named “sort”. This method takes two arguments 1) the array to be sorted 2) Comparator which contains the logic to sort the array Below is the example which sort cards based on…… Continue reading Sorting arrays using Comparator
MultiValuedMap Collection
This post will introduce you to different version of Map called “MultiValuedMap”. This map was added in Apache Commons Collection framework. This map is similar to java.util.Map and at the same different. The similarities are 1) Both map hold key value pair The differences are 2) In java.util.Map the value is a single value whereas…… Continue reading MultiValuedMap Collection
Creating array dynamically
In this post I will explain how to create array dynamically with an example. This is useful when we don’t know the type of array until runtime. Main code 1 package reflection; 2 3 import java.lang.reflect.Array; 4 5 public class ArraysDemo { 6 public static void main(String[] args) { 7 //Creating single dimension array 8…… Continue reading Creating array dynamically
Adding job level listeners using JobExecutionListener
Spring Batch provides a facility to add listeners at job level. The listeners are executed before the job is started and after the job is finished. This post explains how to create listeners and integrate them with the job. We can add our custom processing logic in these listeners and expect them to be called…… Continue reading Adding job level listeners using JobExecutionListener
Creating a temporary file and directory
The post shows you how to create a temporary file and directory in java with an example. Main Code 1 package nio; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 8 public class Example1 { 9 public static void main(String[] args) throws IOException { 10 Path path = Files.createTempFile(“prefix”,…… Continue reading Creating a temporary file and directory
Partitioning a list
Apache Common Collections framework provide a facility to divide a given list into multiple sub list based on a given size. The final list may be smaller than the give size. The below code will show how to do it. 1 import java.util.ArrayList; 2 import java.util.List; 3 4 import org.apache.commons.collections4.ListUtils; 5 6 public class Example5…… Continue reading Partitioning a list
Merging two sorted collections
Apache Common Collections API provide a facility to merge two sorted collections. This post will show how to use it with an example Main Code 1 import java.util.ArrayList; 2 import java.util.List; 3 4 import org.apache.commons.collections4.CollectionUtils; 5 6 public class Example3 { 7 public static void main(String[] args) { 8 List list1 = new ArrayList(); 9…… Continue reading Merging two sorted collections
BidiMap Example
Apache Common Collections framework provide a new map collection called “BidiMap”. This collection is similar to map and at the same time it is different. Similarities 1) Both will contain list of entries each of key value pair 2) By using key we can retrieve the corresponding value Differences 1) In BidiMap we can invert…… Continue reading BidiMap Example