I have 2 classes
A:
public class A {
private int intValue;
private String stringValue;
public A(int intValue, String stringValue) {
this.intValue = intValue;
this.stringValue = stringValue;
}
int getIntValue() {
return intValue;
}
String getStringValue() {
return stringValue;
}
}
and B:
public class B {
private int intValue;
private String stringValue;
B(int intValue, String stringValue) {
this.intValue = intValue;
this.stringValue = stringValue;
}
int getIntValue() {
return intValue;
}
String getStringValue() {
return stringValue;
}
}
and some pretty big array of A objects.
And I want to convert A[] to ArrayList<B> effectively. I know several ways to do this:
final A[] array = {new A(1, "1"), new A(2, "2")/*...*/};
// 1 - old-school
final List<B> list0 = new ArrayList<>(array.length);
for (A a : array) {
list0.add(new B(a.getIntValue(), a.getStringValue()));
}
// 2 - pretty mush same as 1
final List<B> list1 = new ArrayList<>(array.length);
Arrays.stream(array).forEach(a -> list1.add(new B(a.getIntValue(), a.getStringValue())));
// 3 - lambda-style
final List<B> list2 = Arrays.stream(array).map(a -> new B(a.getIntValue(), a.getStringValue())).collect(Collectors.toList());
// 4 - lambda-style with custom Collector
final List<B> list3 = Arrays.stream(array)
.map(a -> new B(a.getIntValue(), a.getStringValue()))
.collect(Collector.of((Supplier<List<B>>)() -> new ArrayList(array.length), List::add, (left, right) -> {
left.addAll(right);
return left;
}));
AFAIK 1 is most efficient. But with java 8 features it's possible to make it shorter. 2 is pretty much the same as 1, but with foreach loop replaced by stream foreach. Not sure about it's effectiveness. 3 is the shortest way, but default Collectors.toList() collector uses default ArrayList::new constructor, which means that array in ArrayList will be resized at least once if we have pretty big initial array. So it's not so efficient. And 4, as I understood it (never used this way though) is pretty much the same as 3, but with single memory allocation for array in ArrayList. But it looks ugly.
So, my question is. Am I right about these 4 ways and their effectiveness? Is there any other short and effective way to do this?