Specifically, I'm looking for similarly clean notation to the Collection<T>.TrueForAll
/ Exists
, etc.
It feels smelly to have to write a foreach loop to inspect the return of a method on each object, so I'm hoping there's a better Java idiom for it.
Predicates are provided in the Google Collections library.
Functional Java provides first-class functions. A predicate is expressed as F<T, Boolean>
. For example, here's a program that tests an array for the existence of a string that is all lowercase letters.
import fj.F;
import fj.data.Array;
import static fj.data.Array.array;
import static fj.function.Strings.matches;
public final class List_exists {
public static void main(final String[] args) {
final Array<String> a = array("Hello", "There", "how", "ARE", "yOU?");
final boolean b = a.exists(matches.f("^[a-z]*$"));
System.out.println(b); // true
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With