I have am X n
two dimensional array of an Object say Foo
. So I have Foo[][] foosArray
. What is the best way to convert this into a List<Foo>
in Java?
This is a nice way of doing it for any two-dimensional array, assuming you want them in the following order:
[[array[0]-elems], [array[1]elems]...]
public <T> List<T> twoDArrayToList(T[][] twoDArray) { List<T> list = new ArrayList<T>(); for (T[] array : twoDArray) { list.addAll(Arrays.asList(array)); } return list; }
Since java-8
List<Foo> collection = Arrays.stream(array) //'array' is two-dimensional .flatMap(Arrays::stream) .collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With