Last updated on February 29th, 2024
Every Java Developer is familiar with NullPointerException.This exception can crash our code and is hard for the developers to handle because developers need too many null checks in the code for handling it. So to resolve this issue Java 8 has introduced the Optional class. This class is available in java.util package. This class can help write neat code and not need too many null checks in the code to handle NullPointerException. By using this class we can specify return alternate values or alternate code to execute. This class makes code more readable.
Prototype:
public final class Optional<T> extends Object{
-------------------
}
Example
Java program example without Optional class:
public class Test {
public static void main(String[] args) {
String[] wordsArray = new String[5];
String word = wordsArray[2].toLowerCase();
System.out.println(word);
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
Example
Optional class in Java 8 example:
import java.util.Optional;
public class Test {
public static void main(String[] args) {
String[] wordsArray = new String[5];
Optional<String> checkNull = Optional.ofNullable(wordsArray[2]);
if (checkNull.isPresent()) {
String word = wordsArray[2].toLowerCase();
System.out.println(word);
} else {
System.out.println("word is null");
}
}
}
Output:
word is null
Methods of Optional class:
public static <T> Optional<T> empty()
→ It returns an empty Optional instance.
public boolean equals(Object obj)
→ This indicates whether some other object is equal to this Optional.
public 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.
public <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
→ If a value is present, apply the provided Optional-bearing mapping function to it, and return that result, otherwise, return an empty Optional.
public T get()
→ If a value is present in this Optional, return the value, otherwise throws NoSuchElementException.
public int hashCode()
→ It returns the hash code value of the present value if any, or 0 (zero) if no value is present.
public void ifPresent(Consumer<? super T> consumer)
→ If a value is present, invoke the specified consumer with the value, otherwise do nothing.
public boolean isPresent()
→ It returns true if there is a value present, otherwise false.
public <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.
public static <T> Optional<T> of(T value)
→ It returns an Optional with the specified present non-null value.
public static <T> Optional<T> ofNullable(T value)
→ It returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
public T orElse(T other)
→ It returns the value if present, otherwise returns the other.
public T orElseGet(Supplier<? extends T> other)
→ Return the value if present, otherwise invoke other and return the result of that invocation.
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
→ It Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.
public String toString()
→ It returns a non-empty string representation of this Optional suitable for debugging.
Conclusion:
This topic is explained What is an Optional class in Java? What is the use of this class? What are the methods of Optional class?