![](/img/trans.png)
[英]How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream
[英]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));
}
我试图使用 Java 8 stream 打印数组,它应该将类型 List<int[]> 返回到主 function。代码中提到了示例输入。
该代码试图创建一个List<int[]>
,但用于创建列表的语法不正确。 要解决此问题,您可以替换以下行:
.collect(Collectors.toList(ArrayList<int[]>::new));
用这一行:
.collect(Collectors.toList());
这将使用默认的List
实现创建一个List<int[]>
,即ArrayList
。
或者,您可以明确指定ArrayList
实现,如下所示:
.collect(Collectors.toCollection(ArrayList::new));
这还将使用ArrayList
实现创建一个List<int[]>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.