isEmpty()
is a ObjectUtils
class that checks if the passed object is empty.
The following types are supported by this method:
The method returns true
if the input object of the type above is empty or points 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 isEmpty(final Object object)
final Object object
: The object to test.
The method returns true
if the input object of the type above is empty or points to null
. Otherwise, the method returns false
.
import org.apache.commons.lang3.ObjectUtils;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;public class Main {static class Temp{int a, b;public Temp(int a, int b) {this.a = a;this.b = b;}}public static void main(String[] args) {List<String> stringList = new ArrayList<>();System.out.println("The output of ObjectUtils.isEmpty() when an empty list is passed is " + ObjectUtils.isEmpty(stringList));Set<String> stringSet = new HashSet<>();System.out.println("The output of ObjectUtils.isEmpty() when an empty set is passed is " + ObjectUtils.isEmpty(stringSet));String input = "";System.out.println("The output of ObjectUtils.isEmpty() when an empty set is passed is " + ObjectUtils.isEmpty(input));Temp temp = new Temp(1, 2);System.out.println("The output of ObjectUtils.isEmpty() when an object of custom class is passed is " + ObjectUtils.isEmpty(temp));stringList.add("hello");System.out.println("The output of ObjectUtils.isEmpty() when a non-empty list is passed is " + ObjectUtils.isEmpty(stringList));}}
List<String> stringList = new ArrayList<>();
The method returns true
when we pass the list above because the list is empty.
Set<String> stringSet = new HashSet<>();
The method returns true
when we pass the set above because the set is empty.
String input = "";
The method returns true
when we pass the string above because the string is empty.
Temp temp = new Temp(1, 2);
The method returns false
when we pass the object above because the method returns false
for custom types.
list = ["hello"]
The method returns false
when we pass the list above because the list is not empty.
The output of ObjectUtils.isEmpty() when an empty list is passed is true
The output of ObjectUtils.isEmpty() when an empty set is passed is true
The output of ObjectUtils.isEmpty() when an empty set is passed is true
The output of ObjectUtils.isEmpty() when an object of custom class is passed is false
The output of ObjectUtils.isEmpty() when a non-empty list is passed is false