masterofdisaster
masterofdisaster

Reputation: 1169

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 ?

Upvotes: 2

Views: 1259

Answers (2)

Andy Turner
Andy Turner

Reputation: 140554

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];
}

Upvotes: 9

shmosel
shmosel

Reputation: 50776

You can use Stream.iterate() to rotate each string incrementally and then convert them to char arrays:

chars = Stream.iterate(alphabet, str -> str.substring(1) + str.charAt(0))
        .limit(alphabet.length())
        .map(String::toCharArray)
        .toArray(char[][]::new);

Or you can rotate each string by its index, with Apache's StringUtils.rotate():

chars = IntStream.range(0, alphabet.length())
        .mapToObj(i -> StringUtils.rotate(alphabet, -i))
        .map(String::toCharArray)
        .toArray(char[][]::new);

Or with Guava's Chars utility class:

chars = Stream.generate(alphabet::toCharArray)
        .limit(alphabet.length())
        .toArray(char[][]::new);

IntStream.range(0, chars.length)
        .forEach(i -> Collections.rotate(Chars.asList(chars[i]), -i));

Upvotes: 2

Related Questions