Double[] instead of double[] variable declarator

Hi everyone i have a problem with java snippet. when i need as output an array of double the snippet automaticaly set Double which doesn’t exist as type.
Possible bug?

Hi @cosimotoma,
have a look at the attached workflow, it contains a Java Snippet node that creates a Double array, this should help you adapt your own workflow:
Double array example.knwf (6.1 KB)

best,
Gabriel

I mean that if i have afunction that creates a primitive type (e.g. double) it can’t be as output but i need to convert it to a Object type like Double and this is resource demanding for large dataset. I’m not an expert in java but i hope it’s more clear now what I mean.

The simplest way to do this conversion is using the Java 8 Streams. Assuming that your double[] is in a variable called d, then

Double[] dbl = DoubleStream.of(d).boxed().toArray(size -> new Double[size]);

will do the conversion.

The details - DoubleStream.of(d) creates a DoubleStream - i.e. a Stream of double primitives, which are then ‘boxed’ to Double objects, and collected into a new array by toArray(size -> new Double[size]. NB if you were to collect the stream simply with toArray(), you would get Object[] rather than Double[].

The only other way is if you have control of the source of your double[] to change it to return Double[] - whichever way you will have to create the objects at some stage!

Hope this helps,

Steve

2 Likes

Thank you, i’ll do this way

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.