Open In App

Optional Class | Guava | Java

Introduction :

Optional is an

immutable object

that may contain a non-null reference to another object. Each instance of this type either contains a non-null reference, or contains nothing, in which case we say that the reference is absent. It is never said to contain null.

Example :

Hashmap.get(key) may return a null when the key is not found in the Hashmap but it may also return a null if the key exists but the value is null.

The Optional class can be used where one might use a null object

. The Optional class has no constructors, but provides

3 public static methods

for acquiring an instance of the class.

Once an instance of Optional has been acquired, there are several instance methods that can be called on that instance. For example,

Optional.isPresent()

method is useful for determining if a given Optional instance has a non-null parameter within it. Once it is known that an Optional instance contains a non-null reference, the

Optional.get()

method returns that stored non-null reference.

Note :

If there is no non-null reference, an exception is thrown upon this method's invocation. So, it is better to call isPresent() first.

Declaration :

@GwtCompatible(serializable = true)
public abstract class Optional<T>
extends Object
implements Serializable

Where, T is the type of instance that can be contained.

Optional Class Methods :

GUAVA_OPTIONAL_1


Example :

// Java code to show implementation of
// Guava Optional class
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Optional;
import static com.google.common.base.Strings.emptyToNull;

class GuavaLibrary {

    // Driver code
    public static void main(String[] args)
    {

        // Creating a List of Strings
        List&lt;String&gt; myList = new ArrayList&lt;String&gt;();

        // Adding values to the Strings
        myList.add(&quot;Geeks&quot;);
        myList.add(&quot;for&quot;);
        myList.add(&quot;GeeksClasses&quot;);
        myList.add(null);
        myList.add(&quot;GeeksforGeeks&quot;);
        myList.add(&quot;&quot;);
        myList.add(&quot;Data Structures&quot;);

        // Displaying values using Java Nulls
        displayValuesUsingJavaNulls(myList);

        // Displaying values using Guava Optional
        displayValuesUsingGuavaOptional(myList);
    }

    // Method to display values using Java Nulls
    public static void displayValuesUsingJavaNulls(List&lt;String&gt; myList)
    {
        System.out.println(&quot;Displaying values using Java Nulls&quot;);

        // For every String in myList
        for (String str : myList) {

            if (str == null || str.isEmpty()) {
                System.out.println(&quot;String : Value is empty or not available&quot;);
            }
            else {
                System.out.println(&quot;String : &quot; + str);
            }
        }
        System.out.println();
    }

    // Method to display values using Guava Optional
    public static void displayValuesUsingGuavaOptional(List&lt;String&gt; myList)
    {
        System.out.println(&quot;Displaying values using Guava Optional&quot;);

        // For each String in myList
        for (String str : myList) {
            Optional&lt;String&gt; optionalName = Optional.fromNullable(emptyToNull(str));
            System.out.println(&quot;String : &quot; + optionalName.or(&quot;String : Value is empty or not available&quot;));
        }
    }
}

Output :

Displaying values using Java Nulls
String : Geeks
String : for
String : GeeksClasses
String : Value is empty or not available
String : GeeksforGeeks
String : Value is empty or not available
String : Data Structures

Displaying values using Guava Optional
String : Geeks
String : for
String : GeeksClasses
String : String : Value is empty or not available
String : GeeksforGeeks
String : String : Value is empty or not available
String : Data Structures

Below given are some other methods provided by Guava Optional Class :

GUAVA_OPTIONAL_2


Guava Optional Class Vs

java.util.Optional

Reference :

Google Guava
Article Tags :