[英]Trying to solve a problem using stream API to find the chunk size. Giving error at .collect(Collectors.toList(ArrayList<int[]>::new)). Java 8+ Stream
IntStream.iterate(0, i -> i + chunkSize)
.limit((long) Math.ceil((double) input.length / chunkSize))
.mapToObj(j -> Arrays.copyOfRange(input, j, j + chunkSize > input.length ? input.length : j + chunkSize))
.collect(Collectors.toList(ArrayList<int[]>::new));
}
I was trying to print array using Java 8 stream and it should return the type List<int[]> to the main function. example input are mentioned in the code.我试图使用 Java 8 stream 打印数组,它应该将类型 List<int[]> 返回到主 function。代码中提到了示例输入。
The code is trying to create a List<int[]>
, but the syntax used to create the list is incorrect.该代码试图创建一个
List<int[]>
,但用于创建列表的语法不正确。 To fix this, you can replace the following line:要解决此问题,您可以替换以下行:
.collect(Collectors.toList(ArrayList<int[]>::new));
with this line:用这一行:
.collect(Collectors.toList());
This will create a List<int[]>
using the default List
implementation, which is ArrayList
.这将使用默认的
List
实现创建一个List<int[]>
,即ArrayList
。
Alternatively, you could specify the ArrayList
implementation explicitly, like this:或者,您可以明确指定
ArrayList
实现,如下所示:
.collect(Collectors.toCollection(ArrayList::new));
This will also create a List<int[]>
using the ArrayList
implementation.这还将使用
ArrayList
实现创建一个List<int[]>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.