Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
supplier
communitycreator

What is ObjectUtils.getFirstNonNull in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

getFirstNonNull() is a staticmethods in Java that can be called without creating an object of the class. method of the ObjectUtils class that executes the provided suppliers in the order they are given and returns the first value that is not null. All subsequent suppliers are not executed after a non-null value is obtained. The method returns null if all of the return values are null or if no suppliers are provided.

A supplier is a functional interface in Java that does not accept any arguments but produces values.

How to import 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;

Syntax


public static <T> T getFirstNonNull(final Supplier<T>... suppliers)

Parameters

final Supplier<T>... suppliers: The list of suppliers to test.

Return value

This method returns the first non-null value from the list of suppliers.

Code

import org.apache.commons.lang3.ObjectUtils;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) {
Supplier<String> stringSupplier = () -> "hello";
Supplier<String> nullSupplier = () -> null;
Supplier<String> supplierPointToNull = null;
String result = ObjectUtils.getFirstNonNull(supplierPointToNull, nullSupplier);
System.out.println("Result of ObjectUtils.getFirstNonNull() with supplierPointToNull and nullSupplier as arguments is " + result);
result = ObjectUtils.getFirstNonNull(nullSupplier, stringSupplier, supplierPointToNull);
System.out.println("Result of ObjectUtils.getFirstNonNull() with nullSupplier, stringSupplier and supplierPointToNull as arguments is " + result);
result = ObjectUtils.getFirstNonNull();
System.out.println("Result of ObjectUtils.getFirstNonNull() with no arguments is " + result);
}
}

In the code above, we define three different suppliers of String datatype:

  • stringSupplier returns a string value.
  • nullSupplier returns a null value.
  • supplierPointToNull points to null.

When supplierPointToNull and nullSupplier are passed as arguments to ObjectUtils.getFirstNonNull(), the method returns null because none of the suppliers return a non-null object.

When nullSupplier, stringSupplier, and supplierPointToNull are passed as arguments to ObjectUtils.getFirstNonNull(), the method returns hello because stringSupplier returns a non-null object.

When no arguments are passed to the ObjectUtils.getFirstNonNull() method, it returns null.

Output

The output of the code will be as follows.


Result of ObjectUtils.getFirstNonNull() with supplierPointToNull and nullSupplier as arguments is null
Result of ObjectUtils.getFirstNonNull() with nullSupplier, stringSupplier and supplierPointToNull as arguments is hello
Result of ObjectUtils.getFirstNonNull() with no arguments is null

RELATED TAGS

java
supplier
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring