Using the isEmpty() Method The isEmpty() method returns true or false depending on whether or not our string contains any text. It's easily chainable with a string == null check, and can even differentiate between blank and empty strings: String string = "Hello there"; if (string == null || string. isEmpty() || string.
The isEmpty() method of List interface in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element. Parameter: It does not accepts any parameter.
The main difference between null and empty is that the null is used to refer to nothing while empty is used to refer to a unique string with zero length. A String refers to a sequence of characters. For example, “programming” is a String. Java programming language supports Strings, and they are treated as objects.
To check if an ArrayList is empty, you can use ArrayList. isEmpty() method or first check if the ArrayList is null, and if not null, check its size using ArrayList. size() method. The size of an empty ArrayList is zero.
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
).
Edit:
You may want to see this question.
I would recommend using Apache Commons Collections
http://commons.apache.org/proper/commons-collections/javadocs/api-release/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());
}
This will throw a NullPointerException
- as will any attempt to invoke an instance method on a null
reference - but in cases like this you should make an explicit check against null
:
if ((test != null) && !test.isEmpty())
This is much better, and clearer, than propagating an Exception
.
No java.util.List.isEmpty()
doesn't check if a list is null
.
If you are using Spring framework you can use the CollectionUtils
class to check if a list is empty or not. It also takes care of the null
references. Following is the code snippet from Spring framework's CollectionUtils
class.
public static boolean isEmpty(Collection<?> collection) {
return (collection == null || collection.isEmpty());
}
Even if you are not using Spring, you can go on and tweak this code to add in your AppUtil
class.
Invoking any method on any null reference will always result in an exception. Test if the object is null first:
List<Object> test = null;
if (test != null && !test.isEmpty()) {
// ...
}
Alternatively, write a method to encapsulate this logic:
public static <T> boolean IsNullOrEmpty(Collection<T> list) {
return list == null || list.isEmpty();
}
Then you can do:
List<Object> test = null;
if (!IsNullOrEmpty(test)) {
// ...
}
In addition to Lion's answer i can say that you better use if(CollectionUtils.isNotEmpty(test)){...}
This also checks for null, so no manual check is not needed.
Yes, it will throw an Exception. maybe you are used to PHP code, where empty($element)
does also check for isset($element)
? In Java this is not the case.
You can memorize that easily because the method is directly called on the list (the method belongs to the list). So if there is no list, then there is no method. And Java will complain that there is no list to call this method on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With