How to determine if a list of string contains null or empty elements

With java 8 you can do:

public String normalizeList(List<String> keys) {
    boolean bad = keys.stream().anyMatch(s -> (s == null || s.equals("")));
    if(bad) {
        //... do whatever you want to do
    }
}

 keys.contains(null) || keys.contains("")

Doesn't throw any runtime exceptions and results true if your list has either null (or) empty String.


This looks fine to me, the only exceptions you would get from keys.contains(null) and keys.contains("") would be if keys itself was null.

However since you check for that first you know that at this point keys is not null, so no runtime exceptions will occur.