From Function<T, Boolean> to t Predicate<T> - What is preferred ?
Brian Goetz
brian.goetz at oracle.com
Mon Dec 31 11:00:59 PST 2012
There is a lot of flexibility permitted when adapting method references
to functional interfaces, for this very reason. So Iters::beginsWithA
should easily convert to either a Predicate<String> or a
Function<String, Boolean>.
Capturing a stateless lambda (static method reference, unbound instance
method reference, or lambda that captures nothing from the environment)
is basically free. Capturing a bound method reference or stateful
lambda is (currently) exactly as costly as creating an inner class
instance.
In the future, we may well be able to optimize these capture costs; we
have a lot of plans on the drawing board, but the implementation
strategy for 8 has already converged. So for the time being, capturing
map::apply is an object creation, whereas capturing Iters::beginWithA is
not.
On 12/31/2012 1:50 PM, Boaz Nahum wrote:
> So Predicate<T> is not Function<T, Boolean>
>
> class Iters {
>
> static boolean beginWithA(String s) {
> return s.startsWith("a");
> }
> }
>
> Function<String, Boolean> map = Iters::beginWithA;
>
> Should we
>
> Predicate<String> filter = Iters::beginWithA;
>
> Or this is not too bad:
> Predicate<String> filter = map::apply
>
> Will optimization come to rescue us in the second case
>
> Thanks
> Boaz
>
More information about the lambda-dev
mailing list