What is ArrayUtils.isNotEmpty in Java?
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.
ArrayUtilsis defined in theApache Commons Langpackage. To addApache Commons Langto the Maven Project, add the following dependency to thepom.xmlfile.
<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
ArrayUtilsclass 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