isNotEmpty()
is a ObjectUtils
class that checks if the object passed as an argument is:
The following types are supported by this method.
The method returns true
if the input object of the above type is not empty or does not point to null
. Otherwise, the method returns false
.
ObjectUtils
The definition of ObjectUtils
can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
You can import the ObjectUtils
class as follows.
import org.apache.commons.lang3.ObjectUtils;
public static boolean isNotEmpty(final Object object)
object
: The object to test.
The method returns true
if the input object of any of the types outlined above is not empty or is not null
. Otherwise, the method returns false
.
package org.apache.commons.lang3;import org.apache.commons.lang3.ObjectUtils;import java.util.ArrayList;import java.util.List;public class main {public static void main(String[] args) {List<String> stringList = new ArrayList<>();System.out.println("The output of ObjectUtils.isNotEmpty() when an empty list is passed is " + ObjectUtils.isNotEmpty(stringList));stringList.add("hello");System.out.println("The output of ObjectUtils.isNotEmpty() when a non-empty list is passed is " + ObjectUtils.isNotEmpty(stringList));stringList = null;System.out.println("The output of ObjectUtils.isNotEmpty() when a null is passed is " + ObjectUtils.isNotEmpty(stringList));}}
The output of ObjectUtils.isNotEmpty() when an empty list is passed is false
The output of ObjectUtils.isNotEmpty() when a non-empty list is passed is true
The output of ObjectUtils.isNotEmpty() when a null is passed is false
List<String> stringList = new ArrayList<>();
The method returns false
when the list above is passed because the list is empty.
list = ["hello"]
The method returns true
when this list is passed because the list is not empty.
null
objectobject = null
The method returns false
as the input object points to null
.