繁体   English   中英

尝试使用 stream API 查找块大小来解决问题。 给出错误 at.collect(Collectors.toList(ArrayList<int[]> ::新的))。 Java 8+ Stream</int[]>

[英]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[]>

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string>

[英]How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> 如何将 Collectors.toList 用于 ArrayList? 有没有办法使用Java 8中的数据流将特定字符描述的许多多行字符串收集到Arraylist中? 收集整数列表(List <Integer>)以使用Java 8 Stream API进行映射 为什么Java ArrayList显式地在序列化流中写入大小字段? ArrayList的大小被parallel stream修改 使用 Java 流检查 ArrayList 内容 Java 8 - 使用 Stream API 使用相同对象填充数组列表的正确方法是什么? 使用Java并行流在非同步ArrayList中添加元素 使用 Java 8 Stream 将通用 arraylist 转换为非通用
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM