-1
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.

1 Answers1

1

The code is trying to create a List<int[]>, but the syntax used to create the list is incorrect. 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.

Alternatively, you could specify the ArrayList implementation explicitly, like this:

.collect(Collectors.toCollection(ArrayList::new));

This will also create a List<int[]> using the ArrayList implementation.

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
ItsMoi
  • 21
  • 3
  • 2
    You can also specify the list’s element type explicitly, i.e. `.collect(Collectors.toCollection(ArrayList::new))`. But the key point is, to use either, `toList()` without arguments or `toCollection(…)` with a factory argument. – Holger Dec 08 '22 at 07:35