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
Category: Apache Common Collections
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
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
Bag Collection
A bag or multiset is a collection that allow duplicate items. It also keeps count of how many duplicate items are present. Apache Collections framework provide an implementation of Bag collection. Below is an example of it. Bag example 1 import org.apache.commons.collections4.Bag; 2 import org.apache.commons.collections4.bag.HashBag; 3 4 public class Example1 { 5 public static void…… Continue reading Bag Collection