javaarraysjava-8java-streamchars

Two dimensional char array from string using Stream


I am about to create the two dimensional array from string:

char[][] chars;
String alphabet = "abcdefghijklmnopqrstuvwxyz";

So the array will ilustrate this matrix: enter image description here

But how can I do that using Java 8 Streams, cause I do not want to do this by casual loops ?


Solution

  • Poor loops. They get such a bad rap. I doubt a streams-based solution would be as clear as this:

    int n = alphabet.length();
    char[][] cs = new char[n][n];
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cs[i][j] = alphabet.charAt((i + j) % n);
      }
    }
    

    If I had to do it with streams, I guess I'd do something like this:

    IntStream.range(0, n)
        .forEach(
            i -> IntStream.range(0, n).forEach(j -> cs[i][j] = alphabet.charAt((i + j) % n)));
    

    Or, if I didn't care about creating lots of intermediate strings:

    cs = IntStream.range(0, n)
        .mapToObj(i -> alphabet.substring(i) + alphabet.substring(0, i))
        .map(String.toCharArray())
        .toArray(new char[n][]);
    

    A slightly more fun way to do it:

    char[][] cs = new char[n][];
    cs[0] = alphabet.toCharArray();
    for (int i = 1; i < n; ++i) {
      cs[i] = Arrays.copyOfRange(cs[i-1], 1, n+1);
      cs[i][n-1] = cs[i-1][0];
    }