Skip to content
Advertisement

How to collect a list of doubles from a stream?

say I desire to collect a list of doubles[] from a lambda function, I have created the following example, however it do not compile given the type. What could I do as a workaround?

JavaScript

Here is the compilation error:

JavaScript

As an attempt I have tried the following

JavaScript

However it prompts this compilation error:

JavaScript

Moreover I have seen (here)[https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html] that fiven side effects it is best to avoid arrays favoring collecting to lists, therefore I must use Arrays with care.

I am not sure exactly how to properly cast or modify my attempts do that I can collect my list of double[].

Any thoughts?

Thanks

Advertisement

Answer

Instead of the .forEach() use .mapToObject(), as forEach doesn’t return anything that could be processed further.

This is the code I would try:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement