Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Stream from a float[]

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?

like image 617
Arnaud Denoyelle Avatar asked Apr 16 '14 10:04

Arnaud Denoyelle


People also ask

How can I Stream be obtained from the given array?

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.

What does Stream () return?

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.

How do you collect a Stream object?

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.


1 Answers

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

DoubleStream ds = IntStream.range(0, floatArray.length)                            .mapToDouble(i -> floatArray[i]); 
like image 132
Brian Goetz Avatar answered Sep 29 '22 10:09

Brian Goetz