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

imparante :

IntelliJ has been telling me that the Double[] is an error: bad return type in method reference: cannot convert Double[] to A[]. I have used double[], removed the ::new static reference, and still the same error. How do I resolve this error? I am attempting to get back 1.000 in the filtered results.

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

Just use .toArray(); without Double[]::new, because your stream hold from beginning an array of double:

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.

I think you are looking to:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=26952&siteId=1