简体   繁体   English

如何使用流从双精度数组中过滤掉非整数值?

[英]How do I filter out non-int values from a double array using stream?

IntelliJ has been telling me that the Double[] is an error: bad return type in method reference: cannot convert Double[] to A[]. IntelliJ 一直告诉我 Double[] 是一个错误:方法引用中的返回类型错误:无法将 Double[] 转换为 A[]。 I have used double[], removed the ::new static reference, and still the same error.我使用了 double[],删除了 ::new 静态引用,但仍然出现相同的错误。 How do I resolve this error?如何解决此错误? I am attempting to get back 1.000 in the filtered results.我试图在过滤结果中取回 1.000。

double[] pricesDoubleArr = {1.000,2.800,4.900};
double[] pricesDoubleFiltered = Arrays.stream(pricesDoubleArr)
                                      .filter(x -> x != Math.round(x))
                                      .toArray(Double[]::new);

Just use .toArray();只需使用.toArray(); without Double[]::new , because your stream hold from beginning an array of double:没有Double[]::new ,因为您的流从双精度数组开始:

double[] doubles = Arrays.stream(pricesDoubleArr)
        .filter(x -> x != Math.round(x))
        .toArray();

I'm not sure what are you looking from your code, but from your question:我不确定你从代码中看什么,但从你的问题来看:

I am attempting to get back 1.000 in the filtered results.我试图在过滤结果中取回 1.000。

I think you are looking to:我认为您正在寻找:

.filter(x -> x == (double) (int) x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM