I have two Classes Class A and Class B. As below:
Class A:
class A {
public String a;
public A() {
}
public A(final String a) {
this.a = a;
}
}
Class B:
class B {
private A a;
private String b;
public B() {
}
public B(final A a) {
this.a = a;
}
}
Where I am converting A[] to B[] as below:
public static void main(final String[] args) {
final A[] myA = new A[3];
myA[0] = new A("a");
myA[1] = new A("b");
myA[2] = new A("c");
B[] myB = new B[myA.length];
IntStream.range(0, myA.length).forEach(propIndex->{
myB[propIndex] = new B(myA[propIndex]);
});
System.out.println(myB.length);
}
Is there any other way to do it without iterating(forEach
) with Index? Something with toArray(B[]::new)
or any other way where I don't have to use forEach
?