What is ArrayUtils.isEmpty in Java?
isEmpty is a static method of the ArrayUtils class that checks whether the given array is empty or null.
Add the apache commons-lang package
ArrayUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added 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.
Importing ArrayUtils
You can import the ArrayUtils class as follows:
import org.apache.commons.lang3.ArrayUtils;
Syntax
public static boolean isEmpty(final int[] array)
Parameters
final int[] array: This parameter is an array to check for emptiness.
Returns
This method returns true if the array is empty or null, and returns false otherwise.
Code
Example 1
array = [1,2,3,4,5]
Applying the isEmpty function will result in false, as the array is not empty.
Example 2
array = []
Applying the isEmpty function will result in true, as the array is empty.
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.isEmpty(array);System.out.println("Array is empty - " + check);int[] array1 = new int[0];check = ArrayUtils.isEmpty(array1);System.out.println("Array1 is empty - " + check);}}
Output
Array is empty - false
Array1 is empty - true