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?
Here's a better way, that doesn't involve copying the data.
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>
andStream<float>
rather than using wrapper types (Stream<Integer>
andStream<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/
from
Java SE 8 for the Really Impatient
by Cay S. Horstmann :