Commons Collections – Inclusion

Apache Commons Collections – Inclusion ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. Checking sublist isSubCollection() method of CollectionUtils can be used to check if a collection contains the given collection or not. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.isSubCollection() method − public static boolean isSubCollection(Collection<?> a, Collection<?> b) Parameters a − The first (sub) collection, must not be null. b − The second (super) collection, must not be null. Return Value True if and only if a is a sub-collection of b. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isSubCollection() method. We”ll check a list is part of another list or not. import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList(“A”,”A”,”A”,”C”,”B”,”B”); List<String> list2 = Arrays.asList(“A”,”A”,”B”,”B”); System.out.println(“List 1: ” + list1); System.out.println(“List 2: ” + list2); System.out.println(“Is List 2 contained in List 1: ” + CollectionUtils.isSubCollection(list2, list1)); } } Output You will receive the following output − List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] Is List 2 contained in List 1: true Print Page Previous Next Advertisements ”;

Commons Collections – Subtraction

Apache Commons Collections – Subtraction ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. Checking Substraction subtract() method of CollectionUtils can be used to get the new collection by subtracting objects of one collection from other. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.subtract() method − public static <O> Collection<O> subtract(Iterable<? extends O> a, Iterable<? extends O> b) Parameters a − The collection to subtract from, must not be null. b − The collection to subtract, must not be null. Return Value A new collection with the results. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.subtract() method. We”ll get the subtraction of two lists. import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList(“A”,”A”,”A”,”C”,”B”,”B”); List<String> list2 = Arrays.asList(“A”,”A”,”B”,”B”); System.out.println(“List 1: ” + list1); System.out.println(“List 2: ” + list2); System.out.println(“List 1 – List 2: “+ CollectionUtils.subtract(list1, list2)); } } Output When you execute the above code, you should see the following output − List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] List 1 – List 2: [A, C] Print Page Previous Next Advertisements ”;

Commons Collections – Discussion

Discuss Apache Commons Collections ”; Previous Next The Apache Commons Collections are the components of the Apache Commons which are derived from Java API and provides component architecture for the Java language. Commons-Collections seek to build upon the JDK classes by providing new interfaces, implementations and utilities. This tutorial covers most of the topics required for a basic understanding of Apache Commons Collections and to get a feel of how it works. Print Page Previous Next Advertisements ”;

Commons Collections – Union

Apache Commons Collections – Union ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. Checking union union() method of CollectionUtils can be used to get the union of two collections. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.union() method − public static <O> Collection<O> union(Iterable<? extends O> a, Iterable<? extends O> b) Parameters a − The first collection, must not be null. b − The second collection, must not be null. Return Value The union of the two collections. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.union() method. We”ll get the union of two lists. import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { //checking inclusion List<String> list1 = Arrays.asList(“A”,”A”,”A”,”C”,”B”,”B”); List<String> list2 = Arrays.asList(“A”,”A”,”B”,”B”); System.out.println(“List 1: ” + list1); System.out.println(“List 2: ” + list2); System.out.println(“Union of List 1 and List 2: “+ CollectionUtils.union(list1, list2)); } } Output This produces the following output − List 1: [A, A, A, C, B, B] List 2: [A, A, B, B] Union of List 1 and List 2: [A, A, A, B, B, C] Print Page Previous Next Advertisements ”;

Commons Collections – Useful Resources

Apache Commons Collections – Useful Resources ”; Previous Next The following resources contain additional information on Apache Commons Collections. Please use them to get more in-depth knowledge on this. Useful Links on Apache Commons Collections Apache Common Collections − Reference for Apache Commons Collections. Commons Collections User Guide − User guide Reference for Apache Commons Collections. Useful Books on Apache Commons Collections To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Commons Collections – Home

Apache Commons Collections Tutorial PDF Version Quick Guide Resources Job Search Discussion The Apache Commons Collections are the components of the Apache Commons which are derived from Java API and provides component architecture for the Java language. Commons-Collections seek to build upon the JDK classes by providing new interfaces, implementations and utilities. This tutorial covers most of the topics required for a basic understanding of Apache Commons Collections and to get a feel of how it works. Audience This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to Apache Commons Collections. Prerequisites Before you start practicing various types of examples given in this reference, we assume that you are already aware about computer programs and computer programming languages. Print Page Previous Next Advertisements ”;

Commons Collections – Safe Empty Checks

Commons Collections – Safe Empty Checks ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. Checking non-empty list isNotEmpty() method of CollectionUtils can be used to check if a list is not empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.isNotEmpty() method − public static boolean isNotEmpty(Collection<?> coll) Parameters coll − The collection to check, may be null. Return Value True if non-null and non-empty. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isNotEmpty() method. We”ll check a list is empty or not. import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = getList(); System.out.println(“Non-Empty List Check: ” + checkNotEmpty1(list)); System.out.println(“Non-Empty List Check: ” + checkNotEmpty1(list)); } static List<String> getList() { return null; } static boolean checkNotEmpty1(List<String> list) { return !(list == null || list.isEmpty()); } static boolean checkNotEmpty2(List<String> list) { return CollectionUtils.isNotEmpty(list); } } Output The output is given below − Non-Empty List Check: false Non-Empty List Check: false Checking empty list isEmpty() method of CollectionUtils can be used to check if a list is empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.isEmpty() method − public static boolean isEmpty(Collection<?> coll) Parameters coll − The collection to check, may be null. Return Value True if empty or null. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.isEmpty() method. We”ll check a list is empty or not. import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = getList(); System.out.println(“Empty List Check: ” + checkEmpty1(list)); System.out.println(“Empty List Check: ” + checkEmpty1(list)); } static List<String> getList() { return null; } static boolean checkEmpty1(List<String> list) { return (list == null || list.isEmpty()); } static boolean checkEmpty2(List<String> list) { return CollectionUtils.isEmpty(list); } } Output Given below is the output of the code − Empty List Check: true Empty List Check: true Print Page Previous Next Advertisements ”;

Commons Collections – Filtering Objects

Commons Collections – Filtering Objects ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. filter() method filter() method of CollectionUtils can be used to filter a list to remove objects which do not satisfy condition provided by predicate passed. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.filter() method − public static <T> boolean filter(Iterable<T> collection, Predicate<? super T> predicate) Parameters collection − The collection to get the input from, may not be null. predicate − The predicate to use as a filter, may be null. Return Value True if the collection is modified by this call, false otherwise. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.filter() method. We”ll filter a list of integer to get even numbers only. import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; public class CollectionUtilsTester { public static void main(String[] args) { List<Integer> integerList = new ArrayList<Integer>(); integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8)); System.out.println(“Original List: ” + integerList); CollectionUtils.filter(integerList, new Predicate<Integer>() { @Override public boolean evaluate(Integer input) { if(input.intValue() % 2 == 0) { return true; } return false; } }); System.out.println(“Filtered List (Even numbers): ” + integerList); } } Output It will produce the following result − Original List: [1, 2, 3, 4, 5, 6, 7, 8] Filtered List (Even numbers): [2, 4, 6, 8] filterInverse() method filterInverse() method of CollectionUtils can be used to filter a list to remove objects, which satisfy condition provided by predicate passed. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.filterInverse() method − public static <T> boolean filterInverse(Iterable<T> collection, Predicate<? super T> predicate) Parameters collection − The collection to get the input from, may not be null. predicate − The predicate to use as a filter, may be null. Return Value True if the collection is modified by this call, false otherwise. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.filterInverse() method. We”ll filter a list of integer to get odd numbers only. import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Predicate; public class CollectionUtilsTester { public static void main(String[] args) { List<Integer> integerList = new ArrayList<Integer>(); integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8)); System.out.println(“Original List: ” + integerList); CollectionUtils.filterInverse(integerList, new Predicate<Integer>() { @Override public boolean evaluate(Integer input) { if(input.intValue() % 2 == 0) { return true; } return false; } }); System.out.println(“Filtered List (Odd numbers): ” + integerList); } } Output The result is as stated below − Original List: [1, 2, 3, 4, 5, 6, 7, 8] Filtered List (Odd numbers): [1, 3, 5, 7] Print Page Previous Next Advertisements ”;

Commons Collections – Overview

Apache Commons Collections – Overview ”; Previous Next Commons Collections augments Java Collections Framework. It provides several features to make collection handling easy. It provides many new interfaces, implementations and utilities. The main features of Commons Collections are as follows − Bag − Bag interfaces simplifies the collections, which have multiple number of copies of each object. BidiMap − BidiMap interfaces provide Bi-Directional maps, which can be used to lookup values using keys or keys using values. MapIterator − MapIterator interface provide simple and easy iteration over maps. Transforming Decorators − Transforming decorators can alter every object of a collection as and when it is added to the collection. Composite Collections − Composite collections are used, where multiple collections are required to be handled uniformly. Ordered Map − Ordered Maps retain the order, in which elements are added in. Ordered Set − Ordered Sets retain the order, in which elements are added in. Reference map − Reference map allows key/values to be garbage collected under close control. Comparator implementations − Many Comparator implementations are available. Iterator implementations − Many Iterator implementations are available. Adapter Classes − Adapter classes are available to convert array and enumerations to collections. Utilities − Utilities are available to test or create typical set-theory properties of collections such as union, intersection. Supports Closure. Print Page Previous Next Advertisements ”;

Commons Collections – Ignore Null

Apache Commons Collections – Ignore Null ”; Previous Next CollectionUtils class of Apache Commons Collections library provides various utility methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This library is very useful prior to jdk 8 as similar functionalities are now provided in Java 8”s Stream API. Check for Not Null Elements addIgnoreNull() method of CollectionUtils can be used to ensure that only non-null values are getting added to the collection. Declaration Following is the declaration for org.apache.commons.collections4.CollectionUtils.addIgnoreNull() method − public static <T> boolean addIgnoreNull(Collection<T> collection, T object) Parameters collection − The collection to add to, must not be null. object − The object to add, if null it will not be added. Return Value True if the collection changed. Exception NullPointerException − If the collection is null. Example The following example shows the usage of org.apache.commons.collections4.CollectionUtils.addIgnoreNull() method. We are trying to add a null value and a sample non-null value. import java.util.LinkedList; import java.util.List; import org.apache.commons.collections4.CollectionUtils; public class CollectionUtilsTester { public static void main(String[] args) { List<String> list = new LinkedList<String>(); CollectionUtils.addIgnoreNull(list, null); CollectionUtils.addIgnoreNull(list, “a”); System.out.println(list); if(list.contains(null)) { System.out.println(“Null value is present”); } else { System.out.println(“Null value is not present”); } } } Output The output is mentioned below − [a] Null value is not present Print Page Previous Next Advertisements ”;