Convert 3 arrays into 1 object array using Streams
Arrays::setAll
Demo:
import java.awt.Color;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int r[] = { 255, 255, 255 };
int g[] = { 0, 0, 0 };
int b[] = { 255, 255, 255 };
Color[] arr = new Color[3];
Arrays.setAll(arr, i -> new Color(r[i], g[i], b[i]));
System.out.println(Arrays.toString(arr));
}
}
Alternatively, you can use IntStream:range to iterate and fill arr
.
import java.awt.Color;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int r[] = { 255, 255, 255 };
int g[] = { 0, 0, 0 };
int b[] = { 255, 255, 255 };
Color[] arr = new Color[3];
IntStream.range(0, arr.length).forEach(i->arr[i] = new Color(r[i], g[i], b[i]));
}
}
IntStream.range
+ mapToObj
then accumulate to an array:
IntStream.range(0, r.length)
.mapToObj(i -> new Color(r[i], g[i], b[i]))
.toArray(Color[]::new);
You can use IntStream.range for index sequence, and then use map for mapping into Color
object and finally collect them into array
IntStream.range(0,r.length)
.boxed()
.map(i->new Color(r[i],g[i],b[i]))
.toArray(Color[]::new);