54

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.

11
  • 3
    Indeed, Java 8 only provides specialized primitive streams for int, long and double, and Arrays.stream does not accept a boolean[]. I guess you will have to box it. Commented Feb 14, 2017 at 11:33
  • 1
    Would using a BitSet be of benefit here instead of using an array? Commented Feb 14, 2017 at 11:36
  • 4
    @Fildor: yes, you can do stream operations on a bit set. The result, however, might be different, as you are streaming over the bits (i.e. their positional number) then. Surprisingly, the obstacles are on the other side: there is no concise way to convert a boolean[] array to a BitSet. Commented Feb 14, 2017 at 11:42
  • 4
    Hope, that linked Q&A still helps. Creating BooleanStream would also imply the creation of PrimitiveIterator.OfBoolean, OptionalBoolean, BooleanFunction<T>, BooleanPredicate (or do we abuse BooleanUnaryFunction for that?), BooleanBinaryOperator, BooleanToIntFunction, BooleanToLongFunction, BooleanToDoubleFunction, BooleanConsumer, (for some unknown reason, BooleanSupplier exists), ObjBooleanConsumer, BooleanSummaryStatistics, etc. Commented Feb 14, 2017 at 12:03
  • 2
    @Holger, BooleanSupplier completes the matrix of (Object|int|long|double|void) -> (Object|int|long|double|void|boolean) functions: all 30 of these actually exist in JDK, and no other one/zero argument functions seems to be provided. So BooleanSupplier looks reasonable. Commented Feb 15, 2017 at 4:28

4 Answers 4

59

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).

Sign up to request clarification or add additional context in comments.

6 Comments

In your opinion, would it be safe to infer that the "lightweight" boxing operations on booleans may be the reason why there are no specialized primitive streams for booleans?
Nice one +1, let's see, may be other ideas will come into people minds :)
@Mena, there are multiple reasons. One of them is code blow-up: every primitive stream adds a big amount of specialized code, not to mention quadratic API grow when you add IntStream.mapToBoolean, BooleanStream.mapToDouble and so on. So they had to stop somewhere. They considered that int, long and double are the most useful primitive types, so it was a reasonable tradeoff. Hopefully Java 10 will bring us generic specialization and Stream<boolean> will work out of the box.
Close as you can get, I guess. But "nice" is different - IMHO. (+1)
@TagirValeev They could have stopped at ... all of the primitives, there aren't that many (8 primitives + 1 for references).
|
12

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.

7 Comments

Nice! Note though that the resulting stream will not be SIZED unless custom spliterator is defined (and it's not defined. This might impact the performance of some operations like toArray(). Also it will not parallelize great (if somebody cares).
@Tagir Valeev: the default spliterator of List “reports Spliterator.SIZED and Spliterator.ORDERED”…
@TagirValeev I've just checked the characteristics of the spliterator returned by Booleans.asList() and it looks like it has a SIZED charateristic.
@Holger, sorry, my bad. Then the only problem is parallelism. Though it's also addressed in Java-9.
@ZhekaKozlov, nope, spliterator trySplit() method must be properly implemented. Default is "poor man parallelism" which traverses list sequentially via iterator into intermediate buffers
|
3

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();

2 Comments

That's three top-level statements + three statement-expressions inside of the for. ,_________,
@Andrey I don't have the phrase to put it another way. Direct is meant in the sense of being put together manually piece by piece. I hope You like solution two more.
2

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.