anyNotNull()
is a ObjectUtils
that accepts a list of objects and checks if the objects in the list are not null.
The method returns false
if all the objects are null, or if the array is null or empty. Otherwise, it returns true
.
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;
public static boolean anyNotNull(final Object... values)
final Object... values
: The list of objects to test.
The anyNotNull
method returns true
if any of the objects in the list are not null. Otherwise, it returns false
.
import org.apache.commons.lang3.ObjectUtils;import java.util.Arrays;public class Main {public static void main(String[] args) {Object[] objects = {"hi", 3, 3.24};System.out.printf("The output of ObjectUtils.anyNotNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.anyNotNull(objects));System.out.println();objects = null;System.out.printf("The output of ObjectUtils.anyNotNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.anyNotNull(objects));System.out.println();objects = new Object[]{null, null, null};System.out.printf("The output of ObjectUtils.anyNotNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.anyNotNull(objects));System.out.println();objects = new Object[]{1.2, null, null};System.out.printf("The output of ObjectUtils.anyNotNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.anyNotNull(objects));System.out.println();objects = new Object[0];System.out.printf("The output of ObjectUtils.anyNotNull() for the string - '%s' is %s", Arrays.toString(objects), ObjectUtils.anyNotNull(objects));System.out.println();}}
The output of the code is as follows:
The output of ObjectUtils.anyNotNull() for the string - '[hi, 3, 3.24]' is true
The output of ObjectUtils.anyNotNull() for the string - 'null' is false
The output of ObjectUtils.anyNotNull() for the string - '[null, null, null]' is false
The output of ObjectUtils.anyNotNull() for the string - '[1.2, null, null]' is true
The output of ObjectUtils.anyNotNull() for the string - '[]' is false
objects = ["hi", 3, 3.24]
The method returns true
because at least one of the objects in the list is not null.
objects = null
The method returns false
because the array/list of objects points to null.
objects = [null, null, null]
The method returns false
because all the values in the array/list of objects are null.
objects = [1.2, null, null]
The method returns true
because at least one of the objects in the list is not null.
objects = []
The method returns false
because the array/list of objects is empty.