I have list of 1D arrays wrapped in an object, I want to collect them in 1 single array using java-8 stream and collect.
Given class Item with an array of
class A{
int x;
}
class Item{
A[] parts=new A[];
public A[] getParts(){
return parts;
}
}
So if I have list l
List<Item> l=new ArrayList<>();
l.add(new Item());
l.add(new Item());
I need to collect the content in these two object in one single array of type A.
So if first Item has parts={1,2} and 2nd Item parts={3,4} the output should be somehow like
A[] out={1,2,3,4}
I tried to do
l.stream().map(Item::getParts).collect(Collectors.toList()).toArray(new A[n]);
but apperantly it has meany problems.
getParts(), notgetparts(). And Stream as a toArray() method, as well as a flatMap() method..Arrays.stream(myArray). Currently, your list is a list of arrays, not a list of elements from the arrays.