Monday, December 14, 2020

Optional ofNullable() method (With Examples)

Oracle Java Certified, Oracle Java Tutorial and Material, Oracle Java Exam Prep, Java Learning

A quick guide to Optional ofNullable() method in Java 8 API with Example programs. How to use in the real time projects.

1. Introduction

In this tutorial, We’ll learn Optional ofNullable() example on how to create new Java 8 Optional object for any value or null value. Optional is part of  java.util package.

API Note: Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.

This method same as  Optional.of() method and additionally supports null values. It does not throw NullPointerException if we pass null to it.

If we are not sure about the value then using  ofNullable() method is suggestable.

Let us see the syntax and example programs.

2. Optional ofNullable Syntax

ofNullable syntax is below.

public static <T> Optional<T> ofNullable(T value)

ofNullable() is a static method and can be accessed directly with the class name.  ofNullable() returns Optional object.

This method accepts any type of value.

3. Java 8 ofNullable() Example

Let us write an example java program on ofNullable().

package com.java.oraclejavacertified.blog.java8.optional;

import java.util.Optional;

/**

 * Java 8 Optional ofNullable() Method Example

 * 

 * @author Venkatesh

 *

 */

public class OptionalOfNullableExample {

 public static void main(String[] args) {

  // A non-null value is passed

  Optional<String> indiaOptional = Optional.ofNullable(getValue("India"));

  System.out.println("indiaOptional : " + indiaOptional);

  // A null value is passed

  Optional<String> usOptional = Optional.ofNullable(getValue("United States"));

  System.out.println("usOptional : " + usOptional);

  System.out.println(indiaOptional.isPresent());

  System.out.println(usOptional.isPresent());

 }

 private static String getValue(String key) {

  if ("India".equals(key)) {

   return "Delhi";

  } else if ("Australia".equals(key)) {

   return "Sidney";

  }

  return null;

 }

}

Output:

indiaOptional : Optional[Delhi]

usOptional : Optional.empty

Assume that getValue() is a third party API method and we do not know the value returns. The returned value may be null or non-null. In such cases, We should use ofNullable() instead of of() method.

In the output, We are seeing the value for the indiaOptional as Delhi. But, seeing Optional.empty for usOptional. Because ofNullable method internally calls  Optional.empty() method if the null value is passed to it.

Internal Implementation Code:

See the internal logic to understand how it is implemented. This method uses two methods. If the value is null, it calls  empty() method. If the value is non-null, it calls  of() method.

public static <T> Optional<T> ofNullable(T value) {

    return value == null ? empty() : of(value);

}

Oracle Java Certified, Oracle Java Tutorial and Material, Oracle Java Exam Prep, Java Learning
Let us take a look at the below example to check value is present using isPresent() method.

System.out.println(indiaOptional.isPresent());
System.out.println(usOptional.isPresent());

Output:

true
false

If you use of() method, it will throw  NullPointerException.

Related Posts

0 comments:

Post a Comment