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.

    Filtering a list using 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 −

    1. public static <T> boolean filter(Iterable<T> collection,
    2. 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.

    1. import java.util.ArrayList;
    2. import java.util.Arrays;
    3. import java.util.List;
    4.  
    5. import org.apache.commons.collections4.CollectionUtils;
    6. import org.apache.commons.collections4.Predicate;
    7.  
    8. public class CollectionUtilsTester {
    9. public static void main(String[] args) {
    10.  
    11. List<Integer> integerList = new ArrayList<Integer>();
    12. integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8));
    13.  
    14. System.out.println("Original List: " + integerList);
    15. CollectionUtils.filter(integerList, new Predicate<Integer>() {
    16.  
    17. @Override
    18. public boolean evaluate(Integer input) {
    19. if(input.intValue() % 2 == 0) {
    20. return true;
    21. }
    22. return false;
    23. }
    24. });
    25.  
    26. System.out.println("Filtered List (Even numbers): " + integerList);
    27. }
    28. }

    Output

    It will print the following result.

    1. Original List: [1, 2, 3, 4, 5, 6, 7, 8]
    2. Filtered List (Even numbers): [2, 4, 6, 8]

    Filtering a list using 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

    1. public static <T> boolean filterInverse(Iterable<T> collection,
    2. 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.

    1. import java.util.ArrayList;
    2. import java.util.Arrays;
    3. import java.util.List;
    4.  
    5. import org.apache.commons.collections4.CollectionUtils;
    6. import org.apache.commons.collections4.Predicate;
    7.  
    8. public class CollectionUtilsTester {
    9. public static void main(String[] args) {
    10.  
    11. List<Integer> integerList = new ArrayList<Integer>();
    12. integerList.addAll(Arrays.asList(1,2,3,4,5,6,7,8));
    13.  
    14. System.out.println("Original List: " + integerList);
    15. CollectionUtils.filterInverse(integerList, new Predicate<Integer>() {
    16.  
    17. @Override
    18. public boolean evaluate(Integer input) {
    19. if(input.intValue() % 2 == 0) {
    20. return true;
    21. }
    22. return false;
    23. }
    24. });
    25.  
    26. System.out.println("Filtered List (Odd numbers): " + integerList);
    27. }
    28. }

    Output

    It will print the following result.

    1. Original List: [1, 2, 3, 4, 5, 6, 7, 8]
    2. Filtered List (Odd numbers): [1, 3, 5, 7]