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.
- Optional.fromNullable(T) allows a null or non-null reference to be provided and wrapped in the new Optional instance.
- If the passed-in parameter is null, then the instance does not have any reference stored and is an “absent” instance.
- If the passed-in parameter is not null, then that non-null reference is stored within the new Optional instance.
- Optional.of(T) which acts like Optional.fromNullable(T), except that it expects a non-null parameter to be passed to it. If null is passed to it, a NullPointerException is thrown.
- Optional.absent() is useful when one has code that knows the parameter that would have been provided to Optional.fromNullable(T) is null and it is more clear to express than an “absent” version of Optional should be returned.
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.
@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 :
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 Class Vs java.util.Optional
- Guava Optional class is serializable, but java.util.Optional is not.
- java.util.Optional has the additional methods ifPresent, filter, flatMap, and orElseThrow.
- java.util.Optional offers the primitive-specialized versions OptionalInt, OptionalLong and OptionalDouble but Guava does not have these.
Reference : Google Guava
- Java 8 Optional Class
- How to avoid NullPointerException in Java using Optional class?
- CaseFormat Class | Guava | Java
- LongMath Class | Guava | Java
- Booleans Class | Guava | Java
- Floats Class | Guava | Java
- Ordering Class | Guava | Java
- Doubles Class | Guava | Java
- Ints Class | Guava | Java
- IntMath Class | Guava | Java
- Longs Class | Guava | Java
- Bytes Class | Guava | Java
- Joiner class | Guava | Java
- Shorts Class | Guava | Java
- Chars Class | Guava | Java