简体   繁体   English

用Java中的流初始化二维数组

[英]Initializing 2D array with streams in Java

I have the following class:我有以下课程:

public class MyClass {
    //...
    public MyClass(int x, int y) {
        //...
    }
}

Now, I need to initialize 2D array with the items:现在,我需要使用以下项目初始化二维数组:

int rows;
int cols;
//initializing rows and cols
MyClass[][] arr = new MyClass[rows][cols];
//how to initialize arr[x][y] with 
//new MyClass(x, y) with streams API

I looked at this example, but it doesn't work in my case: Java 8 Stream and operation on arrays .我看了这个例子,但它在我的情况下不起作用: Java 8 Stream and operation on arrays They use a single IntStream .他们使用单个IntStream

Question: Of course I can use nested for loops, but I think it's now old-style and is considering bad.问题:当然我可以使用嵌套的 for 循环,但我认为它现在是旧式的并且正在考虑不好。 So how to apply streams api and initilize it in Java 8 way?那么如何应用流 api 并以Java 8 的方式初始化它呢?

Streams are not very good at keeping track of index, which you need here. Streams 不太擅长跟踪索引,而您在此处需要用到它。 So you can abuse them like @NicolasFilotto proposes, or in a simpler way:所以你可以像@NicolasFilotto 建议的那样滥用它们,或者以更简单的方式:

MyClass[][] array = new MyClass[rows][cols];
IntStream.range(0, rows)
        .forEach(r -> IntStream.range(0, cols)
                .forEach(c -> array[r][c] = new MyClass(r, c)));

You could even make it look more functional and get rid of the forEach and the mutation part:你甚至可以让它看起来更实用,去掉forEach和 mutation 部分:

MyClass[][] array = IntStream.range(0, rows)
        .mapToObj(r -> IntStream.range(0, cols)
                .mapToObj(c -> new MyClass(r, c))
                .toArray(MyClass[]::new))
        .toArray(MyClass[][]::new);

But honestly, for loops are not obsolete:但老实说, for 循环并没有过时:

for (int r = 0; r < rows; r++) {
    for (int c = 0; c < cols; c++) {
        array[r][c] = new MyClass(r, c);
    }
}

Here is a way to do it:这是一种方法:

int rows = 5;
int cols = 10;
MyClass[][] arr = new MyClass[rows][cols];
Stream.generate(new Supplier<MyClass>() {
    int currentValue = 0;
    @Override
    public MyClass get() {
        MyClass myClass = new MyClass(currentValue / cols, currentValue % cols);
        currentValue++;
        return arr[myClass.x][myClass.y] = myClass;
    }
}).limit(rows * cols).forEach(System.out::println);

Output:输出:

MyClass{x=0, y=0}
MyClass{x=0, y=1}
MyClass{x=0, y=2}
...
MyClass{x=4, y=9}

You can generate a 2D array using two nested IntStream s as follows:您可以使用两个嵌套的IntStream生成二维数组,如下所示:

public static void main(String[] args) {
    int rows = 2, cols = 3;
    // generate an array
    MyClass[][] arr = IntStream.range(0, rows)
            .mapToObj(x -> IntStream.range(0, cols)
                    .mapToObj(y -> new MyClass(x, y))
                    .toArray(MyClass[]::new))
            .toArray(MyClass[][]::new);
    // output
    Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
    //[{x=0, y=0}, {x=0, y=1}, {x=0, y=2}]
    //[{x=1, y=0}, {x=1, y=1}, {x=1, y=2}]
}
public static class MyClass {
    int x, y;

    public MyClass(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "{x=" + x + ", y=" + y + "}";
    }
}

Adaptation for Integer[][] for solution: answered assylias :适应Integer[][]解决方案:回答 assylias

package com.gmail.jackkobec.java.core;

import java.util.Arrays;
import java.util.Random;
import java.util.stream.IntStream;

/**
 * @Author Jack <jackkobec>
 */
public class InitMatrixByStreamApi {
    public static final int ROWS_COUNT = 5; // Matrix rows number
    public static final int COLUMN_COUNT = 2; // Matrix columns number

    public static void main(String[] args) {
        Integer[][] matrixFromStreamApi = initMatrixInStream(ROWS_COUNT, COLUMN_COUNT);
        printMatrix(matrixFromStreamApi);
    }

    /**
     * Init matrix by random int values with border 777
     *
     * @param rowsCount    - Matrix rows number
     * @param columnsCount - Matrix columns number
     * @return Integer[][]
     */
    public static Integer[][] initMatrixInStream(int rowsCount, int columnsCount) {
        return IntStream.range(0, rowsCount)
                .mapToObj(rowArray -> IntStream.range(0, columnsCount)
                        .mapToObj(columnArrayElement -> new Random().nextInt(777))
                        .toArray(Integer[]::new))
                .toArray(Integer[][]::new);
    }

    /**
     * Prints matrix.
     *
     * @param matrix
     */
    public static void printMatrix(Integer[][] matrix) {
        Arrays.stream(matrix)
                .map(array -> Arrays.toString(array) + "\n")
                .forEach(System.out::println);
    }
}

Output:输出:

[395, 58]
[361, 652]
[291, 76]
[112, 77]
[186, 282]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM