otra - Array 2D de Java en ArrayList 2D con transmisión
listas en java netbeans (1)
Así que tengo un Integer[][] data que quiero convertir en un ArrayList<ArrayList<Integer>> , así que traté de usar secuencias y se me ocurrió la siguiente línea:
ArrayList<ArrayList<Integer>> col = Arrays.stream(data).map(i -> Arrays.stream(i).collect(Collectors.toList())).collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new));
Pero la última parte collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new)) me da un error que no puede convertir ArrayList<ArrayList<Integer>> to C
El collect(Collectors.toList() interno collect(Collectors.toList() devuelve una List<Integer> , no ArrayList<Integer> , por lo que debe recopilar estas List s internas en una ArrayList<List<Integer>> :
ArrayList<List<Integer>> col =
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toList()))
.collect(Collectors.toCollection(ArrayList<List<Integer>>::new));
Alternativamente, use Collectors.toCollection(ArrayList<Integer>::new) para recopilar los elementos de la Stream interna:
ArrayList<ArrayList<Integer>> col =
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toCollection(ArrayList<Integer>::new)))
.collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new));