What is ObjectUtils.isEmpty in Java?

isEmpty() is a staticMethods in Java that can be called without creating an object of the class. method of the ObjectUtils class that checks if the passed object is empty.

The following types are supported by this method:

  • CharSequence
  • Array
  • Collection
  • Map

The method returns true if the input object of the type above is empty or points to null. Otherwise, the method returns false.

How to import 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;

Syntax


public static boolean isEmpty(final Object object)

Parameters

final Object object: The object to test.

Return value

The method returns true if the input object of the type above is empty or points to null. Otherwise, the method returns false.

Code

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));
}
}

Example 1: An empty list

  • List<String> stringList = new ArrayList<>();

The method returns true when we pass the list above because the list is empty.

Example 2: An empty set

  • Set<String> stringSet = new HashSet<>();

The method returns true when we pass the set above because the set is empty.

Example 3: An empty string

  • String input = "";

The method returns true when we pass the string above because the string is empty.

Example 4: A custom object

  • Temp temp = new Temp(1, 2);

The method returns false when we pass the object above because the method returns false for custom types.

Example 5: A non-empty list

  • list = ["hello"]

The method returns false when we pass the list above because the list is not empty.

Output


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

Free Resources