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?
The stream(T[] array, int startInclusive, int endExclusive) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with only some of its specific elements. These specific elements are taken from a range of index passed as the parameter to this method.
Streams don't change the original data structure, they only provide the result as per the pipelined methods. Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.
You can use Collectors. toSet() method along with collect() to accumulate elements of a Stream into a Set. Since Set doesn't provide ordering and doesn't allow duplicate, any duplicate from Stream will be discarded and the order of elements will be lost.
Here's a better way, that doesn't involve copying the data.
DoubleStream ds = IntStream.range(0, floatArray.length) .mapToDouble(i -> floatArray[i]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With