8

I would like to count occurrences of a character (for example the space: ' ' ) in 2D Array, using stream. I was trying to find a solution. Here is my code, using a nested loops:

public int countFreeSpaces() {
    int freeSpaces = 0;
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            if (board[j][i] == ' ') freeSpaces++;
        }
    }
    return freeSpaces;
}
6
  • Can you post the code you tried with streams? Commented Jul 13, 2017 at 14:11
  • 1
    what type of array is board? Commented Jul 13, 2017 at 14:11
  • 3
    Possible duplicate of Stream from two dimensional array in java Commented Jul 13, 2017 at 14:12
  • @Kh.Taheri Yes, I tried to implement it to my program, but can not do It because I have char array. Commented Jul 13, 2017 at 14:22
  • 1
    And this for char array: stackoverflow.com/questions/26320910/… Commented Jul 13, 2017 at 14:25

2 Answers 2

9

I believe this answer is slightly more expressive:

int freeSpaces = (int) Arrays.stream(board)
                             .map(CharBuffer::wrap)
                             .flatMapToInt(CharBuffer::chars)
                             .filter(i -> i == ' ')
                             .count();
Sign up to request clarification or add additional context in comments.

3 Comments

Good approach, though I’d use Arrays.stream to stream over an existing array instead of the varargs method Stream.of. Also, String::new implies copying the char[] data, so I’d use .map(CharBuffer::wrap) .flatMapToInt(CharSequence::chars) instead. Contrary to what we might expect, in Java 8, CharBuffer.chars() is even implemented more efficient than String.chars().
first, up-vote. due to I write down the answer I don't know the OP is whether using a char[] or a Character[], so I used IntStream instead. as you can see my answer publish time is before the OP says its type is a char[].
@Holger Thank you very much for your insight! I'll change the answer around.
2

How about this?

//                      v--- create a Stream<char[]>             
int spaces = (int) Stream.of(board)
                          .flatMapToInt(cells->IntStream.range(0, cells.length)
                          .filter(i -> cells[i] == ' '))
                          .count();

5 Comments

:), why is wrong? I don't understand it. could anyone tell me why before I delete the answer? thanks. I'm not good at English.
That works fine, was expecting that it will be like shorter but it is easy to read.
@cerbin if you remove the comments and inline, you can find that will be shorter and expressive.
That is fine, these indentation increases readability. However, In my opinion these comments you made are not necessary, words from java like count() tells enough.
@cerbin okay. I'll remove the comments later.

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.