简体   繁体   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));
}

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

如何转换 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> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何将 Collectors.toList 用于 ArrayList? - How to use Collectors.toList for ArrayList? 有没有办法使用Java 8中的数据流将特定字符描述的许多多行字符串收集到Arraylist中? - Is there a way to collect many multiline strings delineated by a specific character into an Arraylist using the data stream in Java 8? 收集整数列表(List <Integer>)以使用Java 8 Stream API进行映射 - Collect list of Integer (List<Integer>) to map with Java 8 Stream API 为什么Java ArrayList显式地在序列化流中写入大小字段? - Why does Java ArrayList write the size field in serialization stream explicitly? ArrayList的大小被parallel stream修改 - Size of ArrayList modified by parallel stream 使用 Java 流检查 ArrayList 内容 - Check ArrayList content with Java stream Java 8 - 使用 Stream API 使用相同对象填充数组列表的正确方法是什么? - Java 8 - what is the correct way to fill an arraylist with the same object using Stream API? 使用Java并行流在非同步ArrayList中添加元素 - Adding elements in Non-synchronized ArrayList using java parallel stream 使用 Java 8 Stream 将通用 arraylist 转换为非通用 - Convert generic arraylist to non-generic using Java 8 Stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM