Java_8 New - Optional Class
Introduction
Optional is a container object used to contain not-null object. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as available or not available instead of checking null values. It is introduced in java and is similar to what
Optional in
Guava.
Class declaration
Following is the declaration for
java.util.Optional<T> class:
publicfinalclassOptional<T>
extendsObject
Class methods
S.N. | Method & Description |
1 | static <T> Optional<T> empty() Returns an empty Optional instance. |
2 | boolean equals(Object obj) Indicates whether some other object is "equal to" this Optional. |
3 | Optional<T> filter(Predicate<? super T> predicate) If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional. |
4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. |
5 | T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException. |
6 | int hashCode() Returns the hash code value of the present value, if any, or 0 (zero) if no value is present. |
7 | void ifPresent(Consumer<? super T> consumer) If a value is present, invoke the specified consumer with the value, otherwise do nothing. |
8 | boolean isPresent() Return true if there is a value present, otherwise false. |
9 | <U> Optional<U> map(Function<? super T,? extends U> mapper) If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. |
10 | static <T> Optional<T> of(T value) Returns an Optional with the specified present non-null value. |
11 | static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. |
12 | T orElse(T other) Return the value if present, otherwise return other. |
13 | T orElseGet(Supplier<? extends T> other) Return the value if present, otherwise invoke other and return the result of that invocation. |
14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) Return the contained value, if present, otherwise throw an exception to be created by the provided supplier. |
15 | String toString() Returns a non-empty string representation of this Optional suitable for debugging. |
Methods inherited
This class inherits methods from the following classes:
Optional Example
Create the following java program using any editor of your choice in say
C:/> JAVA
Java8Tester.java
import java.util.Optional;
publicclassJava8Tester{
publicstaticvoid main(String args[]){
Java8Tester java8Tester =newJava8Tester();
Integer value1 =null;
Integer value2 =newInteger(10);
//Optional.ofNullable - allows passed parameter to be null.
Optional<Integer> a =Optional.ofNullable(value1);
//Optional.of - throws NullPointerException if passed parameter is null
Optional<Integer> b =Optional.of(value2);
System.out.println(java8Tester.sum(a,b));
}
publicInteger 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.orElse - returns the value if present otherwise returns
//the default value passed.
Integer value1 = a.orElse(newInteger(0));
//Optional.get - gets the value, value should be present
Integer value2 = b.get();
return value1 + value2;
}
}
Verify the result
Compile the class using
javac compiler as follows
C:\JAVA>javac Java8Tester.java
Now run the Java8Tester to see the result
C:\JAVA>java Java8Tester
See the result.
First parameter is present: false
Second parameter is present: true
10