49

I was learning how to use java 8 streams when I noticed something weird.

Arrays.stream() has methods for everything but float arrays :

  • Arrays.stream(int[]) : IntStream
  • Arrays.stream(long[]) : LongStream
  • Arrays.stream(double[]) : DoubleStream

Similarly, there are Stream implementations for int, double etc but not floats :

  • IntStream
  • LongStream
  • DoubleStream

Is there a reason for that?

what is the recommended way to work with float streams?

Reimeus
  • 158,255
  • 15
  • 216
  • 276
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • I was under the impression that they just support the most common primitive types out-of-the-box, but I'm not sure. Maybe you can have a look at the source code and come up with a FloatStream and a stream(float[]): FloatStream method? – Puce Apr 16 '14 at 10:07
  • 9
    from 'Java SE 8 for the Really Impatient' : "If you want to store short, char, byte, and boolean, use an IntStream, and for float, use a DoubleStream. The library designers didn’t think it was worth adding another five stream types." – marcinj Apr 16 '14 at 10:14
  • @marcin_j your comment is worth an answer. Post it and I will accept it. – Arnaud Denoyelle Apr 16 '14 at 10:17
  • Probably because there's no real good reason to use `float` at all, for anything. – ajb Apr 16 '14 at 22:50

3 Answers3

57

Here's a better way, that doesn't involve copying the data.

DoubleStream ds = IntStream.range(0, floatArray.length)
                           .mapToDouble(i -> floatArray[i]);
Brian Goetz
  • 90,105
  • 23
  • 150
  • 161
38

from Java SE 8 for the Really Impatient by Cay S. Horstmann :

2.12. Primitive Type Streams

... If you want to store short, char, byte, and boolean, use an IntStream, and for float, use a DoubleStream. The library designers didn’t think it was worth adding another five stream types.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • 2
    tbh, I'd rather use a Stream than to use an intStream and have to cast int to char every single time I want to do anything with it. – Hakanai Apr 08 '15 at 04:11
3

I asked myself this question. To answer it, I began working on a library that included things like FloatToIntFunction and ByteToCharFunction and (of course) FloatStream,CharStream,ByteStream,etc. It quickly became a headache for me.

It would have been a LOT of work on the part of the library developers to do this because you'd have to create methods and interfaces to to between ALL of the primitive data types. As you implement more data types, it becomes a bigger and bigger mess. Imagine having to implement methods to go from float to all the other primitive types, double to all the other primitive types, char to all the other primitive types, etc.

The long term solution for this is for Java to add value types so that you can do things like Stream<int> and Stream<float> rather than using wrapper types (Stream<Integer> and Stream<Float>)

Take a look at Project Vahalla for updates on this feature which may be added to Java in the future. http://openjdk.java.net/projects/valhalla/

HesNotTheStig
  • 549
  • 1
  • 4
  • 9