27 March 2010

Filtering numbers greater than a value out of a list

Valid since: op4j 1.0

Description
Given a list of numbers, remove all elements greater than a specified value.

Scenario
We have List<Integer> variable called values containing some integer numbers:
// values == LIST [ 6641, 53, 430, 1245 ]
...and we want to remove all the elements greater than 500, so that values looks like this:
// values == LIST [ 53, 430 ]

Recipe
Execute the removeAllTrue(IFunction<T,Boolean>) action, which will remove from the list all the elements for which the specified function returns true. The specified function will be FnNumber.greaterThan(...):

values =
    Op.on(values).removeAllTrue(FnNumber.greaterThan(500)).get();


Comments
op4j includes many boolean comparison function like "eq", "notEq", "greaterThan", "greaterOrEqTo", "lessThan", "lessOrEqTo", etc. They can be applied to a variety of objects like Integers, Longs, BigDecimals, Strings, etc.

No comments:

Post a Comment