In Maps we always use one key object to uniquely identify an entry. We never combined a collection of keys to create one composite key and use it to uniquely identify an entry. But with help of MultiKey class available in Apache Common Collections framework, we can create a composite key to uniquely identify an…… Continue reading Map with Composite key
Parsing xml document with SAX API
In this post I will explain how to parse an xml document in java using the SAX API. SAX api is event based java api. The SAX compliant parser which reads the xml document sequentially, generates events whenever the parser encounters an xml element. The parser reads the document sequentially and doesn’t load the entire…… Continue reading Parsing xml document with SAX API
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