Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

objectutils
java
communitycreator

What is ObjectUtils.isNotEmpty in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

isNotEmpty() is a staticthe methods in Java that can be called without creating an object of the class. method of the ObjectUtils class that checks if the object passed as an argument is:

  • not empty
  • not null

The following types are supported by this method.

  • CharSequence
  • Array
  • Collection
  • Map

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.

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 isNotEmpty(final Object object)

Parameters

object: The object to test.

Return value

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.

Code

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

Output


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

Explanation

Example 1: An empty list

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

The method returns false when the list above is passed because the list is empty.

Example 2: A non-empty list

  • list = ["hello"]

The method returns true when this list is passed because the list is not empty.

Example 3: A null object

  • object = null

The method returns false as the input object points to null.

RELATED TAGS

objectutils
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring