9

Java 8 introduced streams. There are three streams for primitives (IntStream (of int), LongStream (of long), DoubleStream (of double)) and a generic Stream for objects.

I assume that's there's no FloatStream, CharStream, ByteStream, or ShortStream because these are all "subtypes" of existing streams. That is, a stream of short can have each element easily converted to int.

But why is there no BooleanStream?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Ypnypn
  • 933
  • 1
  • 8
  • 21
  • 1
    I don't know if this is relevant, but internally the JVM represents `boolean`s the same way it represents `byte`s. Perhaps they expected you to use `IntStream`? – templatetypedef Sep 17 '14 at 21:03
  • 2
    What would a use case for such stream? – PM 77-1 Sep 17 '14 at 21:08
  • @templatetypedef, except you can promote a `byte` value to `int` because the boolean data type (set of values) is a proper subset of the int data type. In the Java language, unlike certain other languages I know, the boolean type, {`true`, `false`}, is disjoint from the int type. – Solomon Slow Sep 17 '14 at 21:12
  • @PM77-1 I haven't learned streams yet, so I may be way off base here, but bit strings are not unusual in computer science (Huffman codes, for example). – Solomon Slow Sep 17 '14 at 21:13
  • @PM77-1 e.g. to use `collect()` to _xor_ all the values or find the number of `true` values. Also, to convert another stream to an array of booleans. – Ypnypn Sep 17 '14 at 21:15
  • 1
    For my take on this issue see [my answer to a different question](http://stackoverflow.com/a/22497858/1441122). Basically it's in agreement with Brian Goetz's rationale, just stated differently. – Stuart Marks Sep 18 '14 at 01:54
  • 2
    But basically if you really wanted a `BooleanStream` you could use an `IntStream` with values 0 or 1. Of course you could also pack 32 booleans into an `int` or 64 into a `long` if you wanted to improve space efficiency. – Stuart Marks Sep 18 '14 at 01:58
  • @Ypnypn: “find the number of `true` values”: [`bitSet.stream().count()`](http://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html#stream--). Of course, `bitSet.cardinality()` would do as well. Btw. there is nothing what a `BooleanStream` could do what a `Stream` can’t. For collecting into a `boolean[]` you would just need a specialized `Collector`. – Holger Sep 18 '14 at 08:16

0 Answers0