Skip to content
Advertisement

Convert 3 arrays into 1 object array using Streams

Lets say I have the following three arrays:

JavaScript

All arrays will have same length.

I want to convert them into an array of objects of type Color:

JavaScript

Where each index will contain the r,g,b from the same index from the 3 arrays. For example, lets say for Color[1] = new Color(r[1],g[1],b[1]);

How do I do that using Java Streams ?

The for-loop variant of the code is:

JavaScript

Is there even a way to do this using streams ?

Advertisement

Answer

IntStream.range + mapToObj then accumulate to an array:

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