How do I use java.util.Optional class?

The java.util.Optional<T> class is a container object that may or may not contain a non-null value. It was introduced in Java 8 as part of the Java language’s growing emphasis on treating null values as an anti-pattern. Optional is a way of replacing a nullable T reference with a non-null but potentially empty Optional<T> reference.

In functional terminology, Optional is a monadic sequence of operations that can be combined to work with data in a declarative way, while deferring some operations, such as computations on elements.

Here are some useful methods that Optional class provides:

  1. Optional.of(T value): Returns an Optional with the specified present non-null value.
  2. Optional.empty(): Returns an empty Optional instance.
  3. Optional.ofNullable(T value): Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
  4. get(): If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
  5. isPresent(): Returns true if there is a value present, otherwise false.
  6. ifPresent(Consumer<? super T> consumer): If a value is present, invokes the specified consumer with the value, otherwise does nothing.
  7. orElse(T other): Returns the value if present, otherwise returns other.
  8. orElseGet(Supplier<? extends T> other): Returns the value if present, otherwise returns the result produced by the supplying function.
  9. orElseThrow(Supplier<? extends X> exceptionSupplier): If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.

In practical terms, using Optional can help make your code more robust and reduce the likelihood of NullPointerException.

Here is a simple example:

package org.kodejava.util;

import java.util.Optional;

public class OptionalIntroduction {
    public static void main(String[] args) {
        Optional<String> opt = Optional.of("Hello, world!");
        if (opt.isPresent()) {
            System.out.println(opt.get());
        }
    }
}

This program will output: Hello, world!

We can utilize functional-style programming by using ifPresent() method provided by the Optional class. Here’s how:

package org.kodejava.util;

import java.util.Optional;

public class OptionalIfPresent {
    public static void main(String[] args) {
        Optional<String> opt = Optional.of("Hello, world!");
        opt.ifPresent(System.out::println);
    }
}

In this example, opt.ifPresent(System.out::println); is used to print the value of opt if it is present. The System.out::println syntax is a method reference in Java 8 that is functionally equivalent to value -> System.out.println(value). It will only execute System.out.println() if opt is not empty. Hence, it can be considered functional-style programming.

Here are another code snippet on using other methods from the java.util.Optional class:

package org.kodejava.util;

import java.util.Optional;

public class OptionalExample {
    public static void main(String[] args) {
        // Creating Optional objects
        // 1. Creates an empty Optional
        Optional<String> empty = Optional.empty();
        // 2. Creates an Optional with a non-null value
        Optional<String> nonEmpty = Optional.of("Hello");
        // 3. Creates an Optional with a null value
        Optional<String> nullable = Optional.ofNullable(null);

        // isPresent()
        // 1. Output: true
        System.out.println(nonEmpty.isPresent());
        // 2. Output: false
        System.out.println(empty.isPresent());

        // ifPresent()
        // 1. Output: Value is present: Hello
        nonEmpty.ifPresent(value -> System.out.println("Value is present: " + value));
        // 2. No output, since the Optional is empty.
        empty.ifPresent(value -> System.out.println("Value is present: " + value));

        // orElse()
        String valueFromNonEmpty = nonEmpty.orElse("Default Value");
        String valueFromEmpty = empty.orElse("Default Value");
        // Output: Hello
        System.out.println(valueFromNonEmpty);
        // Output: Default Value
        System.out.println(valueFromEmpty);

        // orElseGet()
        String valueFromNonEmptyWithSupplier = nonEmpty.orElseGet(() -> "Default Value");
        String valueFromEmptyWithSupplier = empty.orElseGet(() -> "Default Value");
        // Output: Hello
        System.out.println(valueFromNonEmptyWithSupplier);
        // Output: Default Value
        System.out.println(valueFromEmptyWithSupplier);

        // orElseThrow() when value is present it will return the value
        try {
            String value = nonEmpty.orElseThrow(IllegalArgumentException::new);
            System.out.println(value);
        } catch (IllegalArgumentException e) {
            //Handle exception
            e.printStackTrace();
        }
        // orElseThrow() when value is not present, it throws an exception
        try {
            String value = empty.orElseThrow(IllegalArgumentException::new);
            System.out.println(value);
        } catch (IllegalArgumentException e) {
            //Handle exception
            e.printStackTrace();
        }

    }
}

Output:

true
false
Value is present: Hello
Hello
Default Value
Hello
Default Value
Hello
java.lang.IllegalArgumentException
    at java.base/java.util.Optional.orElseThrow(Optional.java:403)
    at org.kodejava.util.OptionalExample.main(OptionalExample.java:53)

These methods are used to help in providing a more elegant way to handle null values in Java. Make sure to understand how and when to use each method to get the most out of the Optional class.

How to check if an object reference is not null?

Usually, if not always, we use the if statement combined with == or != operators to check if an object reference is null or not. We do this to validate arguments passed to constructors or methods doesn’t contain a null value. These null check can be seen as clutter in our code.

The solution is to use the java.util.Objects class. This static utility class provides methods like requireNonNull(T) and requireNonNull(T, String) to check if the specified object reference is not null. If null, these methods will throw a NullPointerException. Using the second method variant we can customise the exception message.

The example below shows how we use these methods.

package org.kodejava.util;

import java.util.Objects;

public class ObjectsNullCheckDemo {
    private String firstName;
    private String lastName;

    /**
     * Validate constructor arguments. The firstName and lastName
     * arguments can't be null. A NullPointerException with the
     * specified message will be thrown.
     */
    public ObjectsNullCheckDemo(String firstName, String lastName) {
        this.firstName = Objects.requireNonNull(firstName,
                "First name can't be null.");
        this.lastName = Objects.requireNonNull(lastName,
                "Last name can't be null.");
    }

    public static void main(String[] args) {
        // This line is fine.
        ObjectsNullCheckDemo demo = new ObjectsNullCheckDemo("John", "Doe");
        System.out.println("demo = " + demo);

        try {
            // This line produce a NullPointerException
            ObjectsNullCheckDemo demo1 = new ObjectsNullCheckDemo("Alice", null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String name = null;
        try {
            // The line below will throw java.lang.NullPointerException.
            Objects.requireNonNull(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setFirstName(String firstName) {
        // First name can't be null.
        this.firstName = Objects.requireNonNull(firstName,
                "First name can't be null.");
    }

    public void setLastName(String lastName) {
        // Last name can't be null.
        this.lastName = Objects.requireNonNull(lastName,
                "Last name can't be null.");
    }

    @Override
    public String toString() {
        return "ObjectsNullCheckDemo{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

Running the code above will print the following result:

demo = ObjectsNullCheckDemo{firstName='John', lastName='Doe'}
java.lang.NullPointerException: Last name can't be null.
    at java.base/java.util.Objects.requireNonNull(Objects.java:233)
    at org.kodejava.util.ObjectsNullCheckDemo.<init>(ObjectsNullCheckDemo.java:17)
    at org.kodejava.util.ObjectsNullCheckDemo.main(ObjectsNullCheckDemo.java:28)
java.lang.NullPointerException
    at java.base/java.util.Objects.requireNonNull(Objects.java:208)
    at org.kodejava.util.ObjectsNullCheckDemo.main(ObjectsNullCheckDemo.java:36)

How do I wire / inject a null value in Spring?

To wire or inject a null value into a bean we can use the <null> element. The configuration below show you how to do it in the Spring configuration file. You might need to wire a null value for instance when you want to override a value that was autowired into a bean.

As you can see on the configuration below we set the value of property writer in the bean song to a null And we also set a couple null values into the songs property in the album bean.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="song" class="org.kodejava.spring.core.Song">
        <property name="title" value="I Saw Her Standing There" />
        <property name="writer">
            <null />
        </property>
    </bean>

    <bean id="album" class="org.kodejava.spring.core.Album">
        <property name="title" value="Please Please Me" />
        <property name="year" value="1963" />
        <property name="songs">
            <list>
                <ref bean="song" />
                <null />
                <null />
            </list>
        </property>
    </bean>

</beans>