Nulls
Tim Peierls
tim at peierls.net
Fri Sep 21 10:37:44 PDT 2012
On Fri, Sep 21, 2012 at 1:19 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> 4. Provide non-Optional-returning variants that take a non-null default
>> return value to avoid object creation *in some cases*.
>>
>
> It's called: iterator().
>
For some things, yes, but in general, good luck explaining that. For
example, I bet more people would grok what's going on in this:
T maxOr(T defval);
...
Integer m = intStream.maxOr(Integer.MIN_VALUE);
if (m == Integer.MIN_VALUE) {
// empty, no max
} else {
// do something with m
}
than in this:
Iterator<T> max();
...
Iterator<Integer> it = intStream.max();
if (it.hasNext()) {
// do something with it.next()
} else {
// empty, no max
}
Actually, more obvious still would be to use Optional:
Optional<Integer> m = intStream.max();
if (m.isPresent()) {
// do something with m.get()
} else {
// empty, no max
}
I only bring up the others because so many people are worried that the
extra Optional object will never ever be optimized away and will slow
everything down.
--tim
More information about the lambda-libs-spec-observers
mailing list