Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
array
arrayutils
communitycreator

What is ArrayUtils.isNotEmpty in Java?

abhilash

isNotEmpty is a static method of the ArrayUtils class that checks whether the given array is not empty or not null.

Example 1

  • array = [1,2,3,4,5]

Application of the isNotEmpty function will result in true, as the array is not empty.

Example 2

  • array = []

Application of the isNotEmpty function will result in false, as the array is empty.

ArrayUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven Project, add 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 ArrayUtils class as follows.

import org.apache.commons.lang3.ArrayUtils;

Syntax

public static boolean isNotEmpty(final int[] array)

Parameters

  • final int[] array: array to check for non-emptiness.

Return value

This method returns true if the array is not empty or not null. Otherwise it returns false.

Code

import org.apache.commons.lang3.ArrayUtils;

public class Main {

    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,2,3,2,2,4};
        boolean check = ArrayUtils.isNotEmpty(array);
        System.out.println("Array is not empty - " + check);
        int[] array1 = new int[0];
        check = ArrayUtils.isNotEmpty(array1);
        System.out.println("Array1 is not empty - " + check);

    }
}

Expected output

Array is not empty - true
Array1 is not empty - false

RELATED TAGS

java
array
arrayutils
communitycreator
RELATED COURSES

View all Courses

Keep Exploring