2

org.apache.commons.lang3.ArrayUtils provides methods to check if Array is empty, but for example for String array which has empty String elementsmeaning character "" it returns true for that isNotEmpty() method, which is correct as that method check only for null and length == 0.

Is there any other method or implementation which checks also if array has empty element ?

I could do it myself, iterate over array and manualy remove such elements of course, but I would better use any library as apache.

Any hints ?

3 Answers 3

3

With Java 8:

final String[] strings = {"", "Hello", "World"};
final boolean containsEmptyString = Stream.of(strings)
            .anyMatch(string -> string == null || string.isEmpty());
Sign up to request clarification or add additional context in comments.

5 Comments

oh, its nice, but we use for now only 1.7 :(
My bad, but I can only recommend to switch! :-)
I would switch but in JavaEE most AS are not yet supporting 1.8 :(
We switched Glassfish to Wildfly and Java 7 to 8 parallel, but are a small team so less overhead.
We having IBM WAS as we are part of IBM
1

I think you will have to do it manually, perferrably using StringUtils.isNotBlank(str) instead of arrayUtils. So that you don't iterate twice over the array.

Comments

0

So manual way is maybe only solution here in Java1.6 | 1.7 :

public static boolean isEmpty(String[] array){
    for(String tmp : array){
        if(StringUtils.isNotBlank(tmp)){
            return false;
        }
    }
    return true;
}

StringUtils could be from apache, or own code checking for NULL && length ==0

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.