How to get a Stream from a float[]

Solution 1:

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

DoubleStream ds = IntStream.range(0, floatArray.length)
                           .mapToDouble(i -> floatArray[i]);

Solution 2:

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.

Solution 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/