2

I have an array like this

String arr[][] = {{"abc"}, {"bcd"}, {null}}

This is multi dimensional array (single string array with in an array). I want to remove those nulls and want final result as {{"abc"}, {"bcd"}}. This array could be of any size and there can any number of nulls

I tried something like this. I know I can use traditional for loops, but I want to do it using java8 or more efficiently.

 String arr1[][] = Arrays.stream(arr)
            .filter(str -> (((str != null) && (str.length > 0))))
            .toArray(String[][]::new);
4
  • 1
    What did you try? Commented Aug 28, 2019 at 3:47
  • I edited the my question @Naman Commented Aug 28, 2019 at 3:54
  • Not exactly if you look at the braces @Muntasir Commented Aug 28, 2019 at 3:54
  • 1
    By the way, you should avoid writing arrays like this: String arr1[][], you should write String[][] arr1 instead, for the brackets actually belong to the type, rather than the variable name. The reason it is legal in Java is probably because it is an artifact from C. Commented Aug 28, 2019 at 5:44

2 Answers 2

6

You can use streaming from Arrays helper class an filter non-null values:

String arr[][] = {{"abc"}, {"bcd"}, {null}};

String result[][] = Arrays.stream(arr)
    .map(innerArray -> Arrays.stream(innerArray).filter(Objects::nonNull).toArray(String[]::new))
    .toArray(String[][]::new);

Edit:

As @Andreas pointed out, this leaves empty inner arrays, we need to filter them with additional filter(innerArray -> innerArray.length > 0). Finally:

String result[][] = Arrays.stream(arr)
    .map(innerArray -> Arrays.stream(innerArray).filter(Objects::nonNull).toArray(String[]::new))
    .filter(innerArray -> innerArray.length > 0)
    .toArray(String[][]::new);
Sign up to request clarification or add additional context in comments.

6 Comments

That produces [[abc], [bcd], []], not the desired [[abc], [bcd]]
NullPointerException if input is e.g. {{"abc"}, {"bcd"}, null}
Yes, you're passing a null inner array, for {{"abc"}, {"bcd"}, {null}} it works fine. If you want possibility for null arrays, they would have to be filtered first or null check is needed.
Easy enough to add another nonNull filter before the map(...).
That's right, filter(Objects::nonNull) before map would be sufficient. However I won't add it in my answer, as this is not the data in question and adding validation might make the code unreadable. Same thing would happen, when arr == null. But thank you for the discussion :)
|
0

you're almost there with your solution; just one mistake with a check:

String arr1[][] = Arrays.stream(arr)
                        .filter(str -> str[0] != null)
                        .toArray(String[][]::new);

Comments

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.