5

I have Stream Stream<Integer> stream // 1,2,3,4,5,6,7,8,9

I need create int[3][3] from this stream

How can I do it?

I tried

int[][] ints = stream
            .map(i -> new int[]{i})
            .toArray(int[][]::new);

But I get: [[1], [2], [3], [4], [5], [6], [7], [8], [9]]

But I need: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

EDIT: I think it is not dublicate this because I need int[3][3] array and not String[][]

I tried this example

int[][] array =
            IntStream.range(0, 3)
                .mapToObj(x -> IntStream.range(0, 3).boxed()
                    .toArray(Integer[]::new))
                .toArray(int[][]::new);

And I get error

Exception in thread "main" java.lang.ArrayStoreException: [Ljava.lang.Integer;
    at java.util.stream.Nodes$FixedNodeBuilder.accept(Nodes.java:1222)
    at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250)
    at java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:110)
    at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
    at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
    at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
    at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:25)
    at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:4)
    at Main.main(Main.java:12)

EDIT:

int[][] array = stream.collect(() -> new int[3][3],
            (a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {
            });

error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at SparseMatrixSupportImpl.lambda$fromStream$1(SparseMatrixSupportImpl.java:28)
    at java.util.stream.ReduceOps$4ReducingSink.accept(ReduceOps.java:220)
    at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250)
    at java.util.Spliterators$IntArraySpliterator.forEachRemaining(Spliterators.java:1032)
    at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:510)
    at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:27)
    at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:4)
    at Main.main(Main.java:12)



int[] values = new int[]{1, 2, 3,5,6,7,8,9,11};
Stream<Integer> integerStream = Arrays.stream(values).boxed();
2
  • 1
    Possible duplicate of Filling a Multidimensional Array using a Stream Commented Nov 2, 2017 at 10:46
  • 3
    @AntonBalaniuc That might not work if the Stream already exists, e.g. comes from some source OP can not change. Commented Nov 2, 2017 at 10:48

1 Answer 1

4

You can try this code with Java 9 (this throws a compilation error in Java 8, so beware):

int[][] array = Stream.iterate(1, i -> i < 10, i -> i + 1).collect(() -> new int[3][3],
        (a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));

Here is the result:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

With Java 8:

int[][] array = IntStream.range(1, 10).collect(() -> new int[3][3],
    (a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));

Here is another solution which supports numbers other than the pre-defined sequence (1,2,3,4,5,6,7,8,9), but is not that clean as a solution as it uses a counter array:

int[] counter = {0};
int[][] array = Stream.of(4, 2, 3, 4, 5, 8, 9, 8, 11).collect(() -> new int[3][3],
        (a, i) -> {
            a[counter[0] / 3][counter[0] % 3] = i;
            counter[0]++;
        }, (a, i) -> {});

Output:

[4, 2, 3]
[4, 5, 8]
[9, 8, 11]

If you want a bit more generic solution based on the solution above you can try this code snippet which allows to set a variable number of columns:

List<Integer> list = Arrays.asList(4, 2, 3, 4, 5, 8, 9, 8, 11, 12, 13, 17, 32, 45, 89, 91, 91, 98, 87);
int[] counter = {0};
int cols = 5;
int rows = (int) Math.ceil(list.size() / cols) + 1;
int[][] array = list.stream().collect(() -> new int[rows][cols],
        (a, i) -> a[counter[0] / cols][counter[0]++ % cols] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));

The snippet above prints:

[4, 2, 3, 4, 5]
[8, 9, 8, 11, 12]
[13, 17, 32, 45, 89]
[91, 91, 98, 87, 0]
Sign up to request clarification or add additional context in comments.

8 Comments

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
@ip696: Yes, you can get that, if you use other inputs other than 1,2,3,4,5,6,7,8,9. But the snippets above run fine.
I added the original value as I make them
@ip696 See updated solution with counter for alternative with other numbers besides 1,2,3,4,5,6,7,8,9.
but if I need int[N][M] where N = 1000 000 and M = 1000 000?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.