31 May 2010

Checking if all the members of an array meet a specific condition

Valid since: op4j 1.0

Description
Check whether all the elements in an array meet a specific condition, obtaining a TRUE if they do, or a FALSE if they don't.

Scenario
Our Integer[] values variable contains some numbers:
// values == ARRAY [ 100, 23, 587 ]
...and we want to know whether all the numbers are less than 800 or not, obtaining a Boolean object representing the result.

Recipe
Just use operate on the array and use the all action, applying the conditional "lessThan" function:

Boolean result = 
    Op.on(values).all(FnInteger.lessThan(800)).get();

Comments
The all action is also available for lists and sets.

There is also an any action, which returns true if any of the elements meets the specified condition.

No comments:

Post a Comment