I came up with below solution, not sure if Type:safety warnings can be removed using something else.
BiFunction<List<List<T>>, Class<T>,T[][]> toArray = (list,type) ->
{
T a[][] = (T[][]) Array.newInstance(type,
list.size(), list.get(0).size());
IntStream.range(0, a.length)
.forEach(i -> {
a[i]=(T[]) list.get(i).toArray();
});
return a;
};
Also, if this could be improved with one single pipeline I would appreciate the solution.
List.toArray()without an argument will always returnObject[]- you'll need to create proper instances for the components as well, not just for the larger array. Where do you get your warnings?List<List<T>>toT[][]BiFunction<List<List<T>>, Class<T>,T[][]> toArray = (list,type) -> { T a[][] = (T[][]) Array.newInstance(list.get(0).get(0).getClass(), list.size(), list.get(0).size()); IntStream.range(0, list.size()) .forEach(i -> { IntStream.range(0, list.get(0).size()) .forEach(j -> { a[i][j]=list.get(i).get(j); }); }); return a; };