
Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Optional Class
Optional class provides an immutable object which is used to contain a not-null object. Optional object is used to represent null with absent value. This class has various utility methods to facilitate the code to handle values as available or not available instead of checking null values.
Class Declaration
Following is the declaration for com.google.common.base.Optional<T> class −
@GwtCompatible(serializable = true) public abstract class Optional<T> extends Object implements Serializable
Sr.No | Method & Description |
---|---|
1 |
static <T> Optional<T> absent() Returns an Optional instance with no contained reference. |
2 |
abstract Set<T> asSet() Returns an immutable singleton Set whose only element is the contained instance if it is present; an empty immutable Set otherwise. |
3 |
abstract boolean equals(Object object) Returns true if object is an Optional instance, and either the contained references are equal to each other or both are absent. |
4 |
static <T> Optional<T> fromNullable(T nullableReference) If nullableReference is non-null, returns an Optional instance containing that reference; otherwise returns absent(). |
5 |
abstract T get() Returns the contained instance, which must be present. |
6 |
abstract int hashCode() Returns a hash code for this instance. |
7 |
abstract boolean isPresent() Returns true if this holder contains a (non-null) instance. |
8 |
static <T> Optional<T> of(T reference) Returns an Optional instance containing the given non-null reference. |
9 |
abstract Optional<T> or(Optional<? extends T> secondChoice) Returns this Optional if it has a value present; secondChoice otherwise. |
10 |
abstract T or(Supplier<? extends T> supplier) Returns the contained instance if it is present; supplier.get() otherwise. |
11 |
abstract T or(T defaultValue) Returns the contained instance if it is present; defaultValue otherwise. |
12 |
abstract T orNull() Returns the contained instance if it is present; null otherwise. |
13 |
static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals) Returns the value of each present instance from the supplied optionals, in order, skipping over occurrences of absent(). |
14 |
abstract String toString() Returns a string representation for this instance. |
15 |
abstract <V> Optional<V> transform(Function<? super T,V> function) If the instance is present, it is transformed with the given Function; otherwise, absent() is returned. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Creating Optional from Nullable variable
GuavaTester.java
package com.tutorialspoint; import com.google.common.base.Optional; public class GuavaTester { public static void main(String args[]) { Integer value = null; // Optional.fromNullable - allows passed parameter to be null. Optional<Integer> a = Optional.fromNullable(value); // Optional.or - returns the value if present otherwise returns // the default value passed. Integer value1 = a.or(Integer.valueOf(0)); // prints 0 System.out.println("Value: " + value1); } }
Output
Run the GuavaTester and verify the output −
Value: 0
Example - Creating Optional from Non-Nullable variable
GuavaTester.java
package com.tutorialspoint; import com.google.common.base.Optional; public class GuavaTester { public static void main(String args[]) { Integer value = Integer.valueOf(10); // Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.of(value); //Optional.get - gets the value, value should be present Integer value1 = b.get(); // prints 10 System.out.println("Value: " + value1); } }
Output
Run the GuavaTester and verify the output −
Value: 10
Example - Checking if Value is Present or Not
GuavaTester.java
package com.tutorialspoint; import com.google.common.base.Optional; public class GuavaTester { public static void main(String args[]) { GuavaTester guavaTester = new GuavaTester(); Integer value1 = null; Integer value2 = Integer.valueOf(10); //Optional.fromNullable - allows passed parameter to be null. Optional<Integer> a = Optional.fromNullable(value1); //Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.of(value2); System.out.println(guavaTester.sum(a,b)); } public Integer sum(Optional<Integer> a, Optional<Integer> b) { //Optional.isPresent - checks the value is present or not System.out.println("First parameter is present: " + a.isPresent()); System.out.println("Second parameter is present: " + b.isPresent()); //Optional.or - returns the value if present otherwise returns //the default value passed. Integer value1 = a.or(Integer.valueOf(0)); //Optional.get - gets the value, value should be present Integer value2 = b.get(); return value1 + value2; } }
Output
Run the GuavaTester and verify the output −
First parameter is present: false Second parameter is present: true 10