Java 如何从 float[] 中获取 Stream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23106093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:43:54  来源:igfitidea点击:

How to get a Stream from a float[]

javajava-8java-stream

提问by Arnaud Denoyelle

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

当我注意到一些奇怪的事情时,我正在学习如何使用 java 8 流。

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

Arrays.stream()有除浮点数组以外的所有方法:

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

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

同样,有 int、double 等的 Stream 实现,但没有 floats :

  • IntStream
  • LongStream
  • DoubleStream
  • IntStream
  • LongStream
  • DoubleStream

Is there a reason for that?

有什么原因吗?

what is the recommended way to work with float streams?

使用浮动流的推荐方法是什么?

采纳答案by marcinj

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

来自Java SE 8 for the Really ImpatientCay 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.

2.12. 原始类型流

...如果您想存储短、字符、字节和布尔值,请使用 IntStream,对于浮点数,请使用 DoubleStream。库设计者认为不值得再添加五种流类型。

回答by Brian Goetz

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

这是一个更好的方法,它不涉及复制数据。

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

回答by HesNotTheStig

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.

我问自己这个问题。为了回答这个问题,我开始研究一个包含 FloatToIntFunction 和 ByteToCharFunction 以及(当然)FloatStream、CharStream、ByteStream 等内容的库。这很快让我头疼。

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.

库开发人员需要做很多工作才能做到这一点,因为您必须在所有原始数据类型之间创建方法和接口。随着您实现更多的数据类型,它会变得越来越混乱。想象一下,必须实现从 float 到所有其他原始类型、double 到所有其他原始类型、char 到所有其他原始类型等的方法。

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>)

对此的长期解决方案是让 Java 添加值类型,以便您可以执行类似Stream<int>Stream<float>而不是使用包装器类型(Stream<Integer>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/

请查看 Project Vahalla 以获取有关此功能的更新,这些功能将来可能会添加到 Java 中。 http://openjdk.java.net/projects/valhalla/