Andremoniy
Andremoniy

Reputation: 34900

Java-8: boolean primitive array to stream?

There is no nice way to convert given boolean[] foo array into stream in Java-8 in one statement, or I am missing something?

(I will not ask why?, but it is really incomprehensible: why not add stream support for all primitive types?)

Hint: Arrays.stream(foo) will not work, there is no such method for boolean[] type.

Upvotes: 53

Views: 23653

Answers (4)

Kaplan
Kaplan

Reputation: 3728

of course you could create a stream directly

Stream.Builder<Boolean> builder = Stream.builder();
for (int i = 0; i < foo.length; i++)
  builder.add(foo[i]);
Stream<Boolean> stream = builder.build();

…or by wrapping an AbstractList around foo

Stream<Boolean> stream = new AbstractList<Boolean>() {
  public Boolean get(int index) {return (foo[index]);}
  public int size() {return foo.length;}
}.stream();

Upvotes: 2

Nikolas
Nikolas

Reputation: 44398

Skimming through the early access JavaDoc (ie. java.base module) of the newest , there is still no neat way to make the primitive boolean array work with Stream API together well. There is no new feature in the API with treating a primitive boolean array since .

Note that there exist IntStream, DoubleStream and LongStream, but nothing like BooleanStream that would represent of a variation of a sequence of primitive booleans. Also the overloaded methods of Stream are Stream::mapToInt, Stream::mapToDouble and Stream::mapToLong, but not Stream::mapToBoolean returning such hypothetical BooleanStream.

Oracle seems to keep following this pattern, which could be found also in Collectors. There is also no such support for float primitives (there is for double primitives instead). In my opinion, unlike of float, the boolean support would make sense to implement.

Back to the code... if you have a boxed boolean array (ie. Boolean[] array), the things get easier:

Boolean[] array = ...
Stream<Boolean> streamOfBoxedBoolean1 = Arrays.stream(array);
Stream<Boolean> streamOfBoxedBoolean2 = Stream.of(array);

Otherwise you have to use more than one statement as said in this or this answer.

However, you asked (emphasizes mine):

way to convert given boolean[] foo array into stream in Java-8 in one statement.

... there is actually a way to achieve this through one statement using a Spliterator made from an Iterator. It is definetly not nice but :

boolean[] array = ...

Stream<Boolean> stream = StreamSupport.stream(
        Spliterators.spliteratorUnknownSize(
                new Iterator<>() {
                    int index = 0;
                    @Override public boolean hasNext() { return index < array.length; }
                    @Override public Boolean next() { return array[index++]; }
                }, 0), false);

Upvotes: 2

ZhekaKozlov
ZhekaKozlov

Reputation: 39536

You can use Guava's Booleans class:

Stream<Boolean> stream = Booleans.asList(foo).stream();

This is a pretty efficient way because Booleans.asList returns a wrapper for the array and does not make any copies.

Upvotes: 12

Tagir Valeev
Tagir Valeev

Reputation: 100219

Given boolean[] foo use

Stream<Boolean> stream = IntStream.range(0, foo.length)
                                  .mapToObj(idx -> foo[idx]);

Note that every boolean value will be boxed, but it's usually not a big problem as boxing for boolean does not allocate additional memory (just uses one of predefined values - Boolean.TRUE or Boolean.FALSE).

Upvotes: 59

Related Questions