Reputation: 37
I want to convert this final List <List<Object>> datavalues = new ArrayList<>();
into a 2D String array like this String[][]
.
Upvotes: 1
Views: 91
Reputation: 533870
In Java 8 you can do
List<List<Object>> values = ...
String[][] valArr = values.stream()
.map(l -> l.stream()
.map(e -> e.toString())
.toArray(n -> new String[n]))
.toArray(n -> new String[n][]);
As @bohemian points out you can use other lambda forms with
String[][] valArr = values.stream()
.map(l -> l.stream()
.map(Object::toString)
.toArray(String[]::new))
.toArray(String[][]::new);
The problem I have with ::
forms is they can be cooler but more obscure as to what they are doing.
In Java 7, you have to use loops.
Upvotes: 5