servlets struts hibernate jsf spring maven

Java 8 Optional: Complete Reference

Optional is a holder object used to contain not-invalid items. Discretionary item is utilized to speak to invalid with missing quality. This class has different utility techniques to encourage code to deal with esteems as 'accessible' or 'not accessible' rather than checking invalid qualities. It is presented in Java 8 and is like what Optional is in Guava.

This special case happen when you attempt to use an item reference which has not been instated, introduced with invalid or basically does not point to any instance.

Sr.No. Method & Description 1

static <T> Optional<T> empty()

Returns a void Optional instance.


boolean equals(Object obj)

Indicates whether some other item is "equivalent to" this Optional.


Optional<T> filter(Predicate<? super <T> predicate)

If an esteem is available and the esteem coordinates a given predicate, it restores an Optional portraying the esteem, generally restores a void Optional.


<U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)

If an esteem is available, it applies the gave Optional-bearing mapping capacity to it, restores that outcome, generally restores a void Optional.


T get()

If an esteem is available in this Optional, restores the esteem, generally tosses NoSuchElementException.


int hashCode()

Returns the hash code estimation of the present esteem, assuming any, or 0 (zero) if no esteem is present.


void ifPresent(Consumer<? super T> consumer)

If an esteem is available, it conjures the predetermined buyer with the esteem, generally does nothing.


boolean isPresent()

Returns genuine if there is an esteem present, generally false.


<U>Optional<U> map(Function<? super T,? expands U> mapper)

If an esteem is available, applies the gave mapping capacity to it, and if the outcome is non-invalid, restores an Optional depicting the result.


static <T> Optional<T> of(T value)

Returns an Optional with the predefined present non-invalid value.


static <T> Optional<T> ofNullable(T value)

Returns an Optional portraying the predefined esteem, if non-invalid, generally restores a void Optional.


T orElse(T other)

Returns the esteem if present, generally returns other.


T orElseGet(Supplier<? broadens T> other)

Returns the esteem if present, generally conjures other and restores the consequence of that invocation.


<X broadens Throwable> T orElseThrow(Supplier<? broadens X> exceptionSupplier)

Returns the contained esteem, if present, generally tosses an exemption to be made by the gave supplier.


String toString()

Returns a non-void string portrayal of this Optional appropriate for debugging.

This class acquires strategies from the accompanying class −

  • java.lang.Object

Optional Example

import java.util.Optional; 

public class Java8Tester { 

public static void main(String args[]) { 

Java8Tester java8Tester = new Java8Tester(); 

Integer value1 = null; 

Integer value2 = new Integer(10); 

//Optional.ofNullable - permits passed parameter to be null. 

Optional<Integer> a = Optional.ofNullable(value1); 

//Optional.of - tosses NullPointerException whenever passed parameter is null 

Optional<Integer> b = Optional.of(value2); 



public Integer sum(Optional<Integer> a, Optional<Integer> b) { 

//Optional.isPresent - checks the esteem is available or not 

System.out.println("First parameter is available: " + a.isPresent()); 

System.out.println("Second parameter is available: " + b.isPresent()); 

//Optional.orElse - restores the esteem if present generally returns 

//the default esteem passed. 

Integer value1 = a.orElse(new Integer(0)); 

//Optional.get - gets the esteem, esteem ought to be present 

Integer value2 = b.get(); 

return value1 + value2; 



Verify the Result

Compile t

javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition

EmberJS Tutorial