- Commons Collections - Discussion
- Commons Collections - Useful Resources
- Commons Collections - Quick Guide
- Commons Collections - Union
- Commons Collections - Subtraction
- Commons Collections - Intersection
- Commons Collections - Inclusion
- Commons Collections - Safe Empty Checks
- Commons Collections - Filtering Objects
- Commons Collections - Transforming Objects
- Commons Collections - Merge & Sort
- Commons Collections - Ignore Null
- Commons Collections - OrderedMap Interface
- Commons Collections - MapIterator Interface
- Commons Collections - BidiMap Interface
- Commons Collections - Bag Interface
- Commons Collections - Environment Setup
- Commons Collections - Overview
- Commons Collections - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Commons Collections - Filtering Objects
CollectionUtils class of Apache Commons Collections pbrary provides various utipty methods for common operations covering wide range of use cases. It helps avoid writing boilerplate code. This pbrary is very useful prior to jdk 8 as similar functionapties are now provided in Java 8 s Stream API.
filter() method
filter() method of CollectionUtils can be used to filter a pst 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 −
pubpc 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 pst 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; pubpc class CollectionUtilsTester { pubpc 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 pubpc 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 pst to remove objects, which satisfy condition provided by predicate passed.
Declaration
Following is the declaration for
org.apache.commons.collections4.CollectionUtils.filterInverse() method −
pubpc 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 pst 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; pubpc class CollectionUtilsTester { pubpc 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 pubpc 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]Advertisements