In Java 8, the Comparator
class has this really nifty static method that composes a Function
to a Comparator
by using the the result of the Function
as the input for the Comparator
.
What I would like to do, is be able to compose Function
objects with other types like Predicate
, in order to make my code more readable, and to make my functional operations more powerful.
For example, say there is a Set<Person>
where Person
has a public String getName()
method. I want to be able to filter out the Person
objects that don't have a name. Ideally, the syntax would look like this:
people.removeIf(Predicates.andThenTest(Person::getName, String::isEmpty));
Are there any built-in methods that can compose a Function
with something like a Predicate
? I'm aware of Function#andThen(Function)
, but that is only useful for combining functions with other functions, and sadly, Predicate
doesn't extend Function<T, Boolean>
.
P.S. I'm also aware that I could use a lambda like p -> p.getName().isEmpty()
, but I would like for a way to compose pre-existing Predicate
objects with a Function
.