How do you create stream of Boolean.FALSE
, say, length of 100?
What I've struggled with is:
Boolean.FALSE
. But new Boolean[100]
returns an array of NULL
. So reasonably I considered to use stream API as a convenient Iterable
and almost (1) Iterable
manipulation tool;Boolean
no-params constructor (2), hence I can't
use Stream.generate()
, since it accepts Supplier<T>
(3).What I found is Stream.iterate(Boolean.FALSE, bool -> Boolean.FALSE).limit(100);
gives what I want, but it doesn't seem to be quite elegant solution, IMHO.
One more option, I found (4) is IntStream.range(0, 100).mapToObj(idx -> Boolean.FALSE);
, which seems to me even more strange.
Despite these options don't violate pipeline conception of a stream API, are there any more concise ways to create stream of Boolean.FALSE
?
Even though Boolean
has no no-arg constructor, you can still use Stream.generate
using a lambda:
Stream.generate(() -> Boolean.FALSE).limit(100)
This also has the advantage (compared to using a constructor) that those will be the same Boolean
instances, and not 100 different but equal ones.
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