How to create a two dimensional array from a stream in Java 8?
I have a text file like this: ids.txt 1000 999 745 123 ... I want to read this file and load it in a two dimensional array. I expect to have an array similar to the one below: Object[][] data = new Object[][] { // { new Integer(1000) }, // { new Integer(999) }, // { new Integer(745) }, // { new Integer(123) }, // ... }; Here is the code I wrote: File idsFile = ... ; try (Stream<String> idsStream = Files.lines(idsFile.toPath(), StandardCharsets.US_ASCII)) { Object[][] ids = idsStream .filter(s -> s.trim().length() > 0) .toArray(size -> new Object[size][]); // Process ids array here... } When