Convert nested list to 2d array

后端 未结 4 1811
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 19:07

I\'m trying to convert a nested list into a 2d array.

List> list = new ArrayList<>();

list.add(Arrays.asList(\"a\", \"b\", \"         


        
4条回答
  •  温柔的废话
    2020-12-09 19:45

    You could do this:

    String[][] array = list.stream()
        .map(l -> l.stream().toArray(String[]::new))
        .toArray(String[][]::new);
    

    It creates a Stream> from your list of lists, then from that uses map to replace each of the lists with an array of strings which results in a Stream, then finally calls toArray(with a generator function, instead of the no-parameter version) on that to produce the String[][].

提交回复
热议问题