Is there any decent way to convert multidimensional array to List<List<T>>?

Aashish Pawar :

when we have to convert single-dimensional array List type, we can simply use Arrays.asList(). But in the context of multidimensional arrays, I did not find anything similar to this. I have to manually create a List<List<T>> and fill values as per original multidimensional array. Is there any decent way to convert between these types? Java 8 way?

Andronicus :

You can use Arrays.stream and map inner arrays:

<T> List<List<T>> toNestedList(T[][] array) {
    return Arrays.stream(array)
        .map(Arrays::asList)
        .collect(Collectors.toList());
}

This will create a stream of inner arrays, map them to list and collect all inner lists to an outer one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325653&siteId=1