java - جافا 2D مجموعة في 2D أريليست مع تيار
arrays arraylist (1)
لذا لدي بيانات Integer[][] data
أريد أن أحولها إلى ArrayList<ArrayList<Integer>>
، لذا حاولت استخدام مجموعات البث وظهرت مع السطر التالي:
ArrayList<ArrayList<Integer>> col = Arrays.stream(data).map(i -> Arrays.stream(i).collect(Collectors.toList())).collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new));
ولكن الجزء الأخير collect(Collectors.toCollection(ArrayList<ArrayList<Integer>>::new))
يعطيني خطأ أنه لا يمكن تحويل ArrayList<ArrayList<Integer>> to C
تعيد المجموعة الداخلية collect(Collectors.toList()
List<Integer>
، وليس ArrayList<Integer>
، لذا يجب عليك جمع هذه List
الداخلية في ArrayList<List<Integer>>
ليست 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));
بدلا من ذلك، استخدم Collectors.toCollection(ArrayList<Integer>::new)
لجمع عناصر Stream
الداخلي:
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));