Is there any utility method to convert a list of Numerical types to array of primitive type? In other words I am looking for a better solution than this.
private long[] toArray(List<Long> values) {     long[] result = new long[values.size()];     int i = 0;     for (Long l : values)         result[i++] = l;     return result; } 
                The best and easiest way to convert a List into an Array in Java is to use the . toArray() method. Likewise, we can convert back a List to Array using the Arrays. asList() method.
Create a List object. Add elements to it. Create an empty array with size of the created ArrayList. Convert the list to an array using the toArray() method, bypassing the above-created array as an argument to it.
Convert your array to a List with the Arrays. asList utility method. Integer[] numbers = new Integer[] { 1, 2, 3 }; List<Integer> list = Arrays. asList(numbers);
Since Java 8, you can do the following:
long[] result = values.stream().mapToLong(l -> l).toArray();   What's happening here?
List<Long> into a Stream<Long>.mapToLong on it to get a LongStream  mapToLong is a ToLongFunction, which has a long as the result type.Long to a long, writing l -> l as the lambda expression works. The Long is converted to a long there. We could also be more explicit and use Long::longValue instead.toArray, which returns a long[] If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With