Verifying the exact number of method invocations

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

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

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