If you use the Apache Commons Collections library in your project, you may use the CollectionUtils.isEmpty(...) and MapUtils.isEmpty(...) methods which respectively check if a collection or a map is empty or null (i.e. they are "null-safe").
The code behind these methods is more or less what user @icza has written in his answer.
Regardless of what you do, remember that the less code you write, the less code you need to test as the complexity of your code decreases.
Answer from Jalayn on Stack OverflowIf you use the Apache Commons Collections library in your project, you may use the CollectionUtils.isEmpty(...) and MapUtils.isEmpty(...) methods which respectively check if a collection or a map is empty or null (i.e. they are "null-safe").
The code behind these methods is more or less what user @icza has written in his answer.
Regardless of what you do, remember that the less code you write, the less code you need to test as the complexity of your code decreases.
That is the best way to check it. You could write a helper method to do it:
public static boolean isNullOrEmpty( final Collection< ? > c ) {
return c == null || c.isEmpty();
}
public static boolean isNullOrEmpty( final Map< ?, ? > m ) {
return m == null || m.isEmpty();
}
You're trying to call the isEmpty() method on a null reference (as List test = null;). This will surely throw a NullPointerException. You should do if(test!=null) instead (checking for null first).
The method isEmpty() returns true, if an ArrayList object contains no elements; false otherwise (for that the List must first be instantiated that is in your case is null).
You may want to see this question.
I would recommend using Apache Commons Collections:
https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/CollectionUtils.html#isEmpty-java.util.Collection-
which implements it quite ok and well documented:
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
Avoid null.
return resultList == null ? Collections.emptyList() : resultList;
And then check the list with list.isEmpty().
Optional is redundant and more or less functions as a collection of 0/1 elements, as unpacking is needed, and then one still has a collection.
One might sometimes consider returning a Stream, if the result must be requeried every time.
return result == null ? Stream.empty() : resultList.stream();
retVal = Optional.ofNullable(resultList).filter(l -> !l.isEmpty()).isPresent();
You can get the result that you're seeing if the list contains a single zero-length string:
List<String> list = new ArrayList<String>();
list.add("");
System.out.println("blah = " + list); // displays "blah = []"
if (list.isEmpty()) {
System.out.println("Empty"); // doesn't get displayed
}
probably because
if (selectorBean.getSelectedPriorities().isEmpty()){
System.out.println("IS EMPTY");
}
needs a return?
keys.contains(null) || keys.contains("")
Doesn't throw any runtime exceptions and results true if your list has either null (or) empty String.
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
}
}