Open In App

Java Array Empty Check

Last Updated : 09 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, an array is considered non-empty, if it is not null and its length is greater than 0. It is important to check if an array is not empty before performing operations on it to avoid NullPointerException or unnecessary processing.

Example: The simplest way to check if an array is not empty is by verifying that the array is not null and the array length is greater than zero.

Java
// Java Program to check if an Array 
// is Not Empty in Java
public class ArrayNotEmptyCheck {
  
    public static void main(String[] args) {
      
        int[] arr = {1, 2, 3};

        // Check if the array is not null and 
        // its length is greater than 0
        if (arr != null && arr.length > 0) {
            System.out.println("The array is not empty.");
        } 
      else {
            System.out.println("The array is empty.");
        }
    }
}

Output
The array is not empty.

Other Methods to Check if an Array is Not Empty

1. Using a Utility Function

For a reusable solution, we can encapsulate the null and length checks in a utility function. This makes the code cleaner and reusable across multiple scenarios.

Java
// Java Program to Check if array is 
// not empty using utility method
public class ArrayUtils {
  
    // Utility method to check if an array is not empty
    public static boolean isArrayNotEmpty(int[] arr) {
      
        // Check if the array is not null 
        // and has more than 0 elements
        return arr != null && arr.length > 0;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};

        // Check if the array is not empty 
        // using the utility method
        if (isArrayNotEmpty(arr)) {
            System.out.println("The array is not empty.");
        } else {
            System.out.println("The array is empty.");
        }
    }
}

Output
The array is not empty.

Explanation: The isArrayNotEmpty method checks both conditions (array != null and array.length > 0) and returns a boolean result.

2. Using Streams (Java 8+)

Java 8 introduced streams, which can also be used to check if an array contains elements. While this method is more complex for basic checks, it is useful when we want to extend the logic (e.g., filtering elements).

Java
// Java Program to check if array 
// is not empty using streams
import java.util.Arrays;

public class ArrayCheckWithStreams {
  
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};

        // Check if the array is not null and contains 
        // at least one element using streams
        if (arr != null && Arrays.stream(arr).findAny().isPresent()) {
            System.out.println("The array is not empty.");
        } else {
            System.out.println("The array is empty.");
        }
    }
}

Output
The array is not empty.

Explanation: The Arrays.stream(array) converts the array into a stream and the .findAny().isPresent() checks if any element is present in the stream.


Next Article
Practice Tags :

Similar Reads